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

VulpesRank #55 · 29.2K reputationPosted Aug 12, 2013, 12:14 PM EDT
The output is:
VulpesRank #55 · 29.2K reputationPosted Aug 9, 2013, 12:24 AM EDT
string regExp = @"^(?:[A-Za-z]\:|\\|\.\.)(\\[a-zA-Z_\-\s0-9\.]+)+(\.\w+)?$";
David SmithRank #999 · 2K reputationPosted Aug 12, 2013, 6:09 AM EDT
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"
VulpesRank #55 · 29.2K reputationPosted Aug 12, 2013, 6:39 AM EDT
string regExp = @"^(?:[A-Za-z]\:|\\\.\.|\.\.|\\)(\\|\\[a-zA-Z_\-\s0-9\.]+)+(\.\w+)?$";
David SmithRank #999 · 2K reputationPosted Aug 12, 2013, 8:31 AM EDT
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.");
}
David SmithRank #999 · 2K reputationPosted Aug 12, 2013, 2:09 PM EDT
VulpesRank #55 · 29.2K reputationPosted Aug 12, 2013, 3:04 PM EDT
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 SmithRank #999 · 2K reputationPosted Aug 12, 2013, 10:23 PM EDT
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.");
}
VulpesRank #55 · 29.2K reputationPosted Aug 12, 2013, 11:04 PM EDT
"\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 SmithRank #999 · 2K reputationPosted Aug 12, 2013, 11:56 PM EDT
"..\\Temp\\Input\\Example.xml"
David SmithRank #999 · 2K reputationPosted Aug 13, 2013, 12:35 AM EDT
".\\Input\\Example.xml"