Dan Dan - 4 years ago 92
C# Question

C# - Regex for file paths e.g. C:\test\test.exe

I am currently looking for a regex that can help validate a file path e.g.:


Answer Source

I decided to post this answer which does use a regular expression.


Works for these:


Edit: Added example usage:

if (Regex.IsMatch (text, @"^(?:[a-zA-Z]\:|\\\\[\w\.]+\\[\w.$]+)\\(?:[\w]+\\)*\w([\w.])+$"))
  // Valid

*Edit: * This is an approximation of the paths you could see. If possible, it is probably better to use the Path class or FileInfo class to see if a file or folder exists.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download