5

I wanted to validate a file name along with its full path. I tried certain Regular Expressions as below but none of them worked correctly.

^(?:[\w]\:|\\)(\\[a-z_\-\s0-9\.]+)+\.(txt|gif|pdf|doc|docx|xls|xlsx)$
and
^(([a-zA-Z]\:)|(\\))(\\{1}|((\\{1})[^\\]([^/:*?<>""|]*))+)$
etc...

My requirement is as mentioned below: Lets say if the file name is "c:\Demo.txt" then it should check every possibilites like no double slash should be included(c:\\Demo\\demo.text) no extra colon like(c::\Demo\demo.text). Should accept UNC files like(\\staging\servers) and others validation as well. Please help. I am really stuck here.

Allon Guralnek
  • 15,813
  • 6
  • 60
  • 93
Running Rabbit
  • 2,634
  • 15
  • 48
  • 69
  • not homework actually. Some important work has been stuck bcoz of this. – Running Rabbit Aug 02 '12 at 12:11
  • 1
    If your file is uploaded then are you really concerned by the path name or only on the filename part? – Steve Aug 02 '12 at 12:16
  • File is getting uploaded, as their are no validation applied to it. I just want to provide validation to the file name. Can you help me? – Running Rabbit Aug 02 '12 at 12:43
  • possible duplicate of [In C# check that filename is *possibly* valid (not that it exists)](http://stackoverflow.com/questions/422090/in-c-sharp-check-that-filename-is-possibly-valid-not-that-it-exists) – Patrick McDonald Aug 02 '12 at 14:56
  • [This question](http://stackoverflow.com/questions/6063418/need-help-with-a-file-path-validation-regular-expression) contains a partial answer. – Allon Guralnek Aug 02 '12 at 21:46

2 Answers2

2

Why are you not using the File class ? Always use it !

File f = null;
string sPathToTest = "C:\Test.txt";
try{
f = new File(sPathToTest );
}catch(Exception e){
   Console.WriteLine(string.Format("The file \"{0}\" is not a valid path, Error : {1}.", sPathToTest , e.Message);
}

MSDN : http://msdn.microsoft.com/en-gb/library/system.io.file%28v=vs.80%29.aspx

Maybe you're just looking for File.Exists ( http://msdn.microsoft.com/en-gb/library/system.io.file.exists%28v=vs.80%29.aspx )

Also take a look to the Path class ( http://msdn.microsoft.com/en-us/library/system.io.path.aspx )

The GetAbsolutePath could be one way to get what you want! ( http://msdn.microsoft.com/en-us/library/system.io.path.getfullpath.aspx )

string sPathToTest = "C:\Test.txt";
string sAbsolutePath = "";
try{
   sAbsolutePath = Path.GetAbsolutePath(sPathToTest);
   if(!string.IsNullOrEmpty(sAbsolutePath)){
     Console.WriteLine("Path valid");
   }else{
     Console.WriteLine("Bad path");
   }
}catch(Exception e){
   Console.WriteLine(string.Format("The file \"{0}\" is not a valid path, Error : {1}.", sPathToTest , e.Message);

}
ykatchou
  • 3,667
  • 1
  • 22
  • 27
0

If you are interested only in the filename part (and not the whole path because you get the file via upload) then you could try something like this:

string uploadedName =  @"XX:\dem<<-***\demo.txt";

int pos = uploadedName.LastIndexOf("\\");
if(pos > -1)
    uploadedName = uploadedName.Substring(pos+1);

var c = Path.GetInvalidFileNameChars();
if(uploadedName.IndexOfAny(c) != -1)
     Console.WriteLine("Invalid name");
else
     Console.WriteLine("Acceptable name");

This will avoid the use of Exceptions as method to drive the logic of your code.

Steve
  • 213,761
  • 22
  • 232
  • 286