Below I am using this regex below to validate relative paths. The regex string below do not accept this as a valid path. Can someone help me.
"..\\..\\Bin\\Example.xml" Error, not recognized as a path according to the regex below.
string regExp = @"^(?:[A-Za-z]\:|\\)(\\[a-zA-Z_\-\s0-9\.]+)+(\.\w+)?$";
Loading
VulpesPosted Aug 12, 2013, 4:14 PM
The output is:
David SmithPosted Aug 13, 2013, 4:35 AM
".\\Input\\Example.xml"
David SmithPosted Aug 13, 2013, 3:56 AM
"..\\Temp\\Input\\Example.xml"
VulpesPosted Aug 13, 2013, 3:04 AM
"\input\example.txt"
won't exist.
You'll either have to use:
"input\example.txt"
or:
".\input\example.txt" // note, just one dot
as I said in my previous post.
David SmithPosted Aug 13, 2013, 2:23 AM
But for some reason. I am getting the directory do not exist.
When I set the string to "\input\example.txt"
I'm not understanding why it keep jumping into the case the directory does not exist. If you view the screen shot it does exist.
FileInfo fileInfo = new FileInfo(fileName);
//Verify if directory exists on local disks.
if (!Directory.Exists(fileInfo.DirectoryName))
{
throw new ArgumentNullException("fileInfo.DirectoryName", "Specified directory does not exist. Please specify a existing directory.");
}
VulpesPosted Aug 12, 2013, 7:04 PM
input\example.xml
or
.\input\example.xml
It would be easy to allow for the second one but major surgery would be needed to allow for the first one.
Are you wanting here a regular expression that deals with relative paths in isolation or one that can deal with both absolute and relative paths?
Also are you wanting to allow for the possible use of '/' rather than '\' which is another awkward point to deal with?
If you can clarify, I'll have another think about it tomorrow as it's past midnight here now.
David SmithPosted Aug 12, 2013, 6:09 PM
David SmithPosted Aug 12, 2013, 12:31 PM
I am still getting an error. Try this out below.
string regExp = @"^(?:[A-Za-z]\:|\\\.\.|\.\.|\\)(\\|\\[a-zA-Z_\-\s0-9\.]+)+(\.\w+)?$";
Regex r= new Regex(regExp);
if (!r.IsMatch("//Input//test.xml))
{
throw new ArgumentNullException("fileName", "A valid filepath/filename is required. Please specify a valid filepath/filename.");
}
VulpesPosted Aug 12, 2013, 10:39 AM
string regExp = @"^(?:[A-Za-z]\:|\\\.\.|\.\.|\\)(\\|\\[a-zA-Z_\-\s0-9\.]+)+(\.\w+)?$";
David SmithPosted Aug 12, 2013, 10:09 AM
What if a user type Backslash Backslash first before the two dots below.
At the moment this does not work:
"\\..\\..\\Bin\\Test.xml"
This works:
"..\\..\\Bin\\Example.xml"
VulpesPosted Aug 9, 2013, 4:24 AM
string regExp = @"^(?:[A-Za-z]\:|\\|\.\.)(\\[a-zA-Z_\-\s0-9\.]+)+(\.\w+)?$";