Check Path Format

Posted on 2006-04-09
Medium Priority
Last Modified: 2008-01-09

My ASP.NET (c#) pulls a folder form the web.config file fine.  However i need to check thi sfolder for format etc.

So for example if an administrator enters the folder "C:\temp" i convert it to "C:\\temp\\" as i then add a file to that directory.  Likewise if "C:\\temp\\" is entered i leave it alone or "C:\\temp" i make it "C:\\temp\\"

Is there an easy way of doing this


Question by:wbcintsol
LVL 19

Expert Comment

by:Fahad Mukhtar
ID: 16414708
you can use the @ operator .. with @-quoting, escape sequences are not processed
assuming the variable that contains the web.config value is path then
path += !path.EndsWith(@"\") ? @"\" : "";

Accepted Solution

VovinE earned 2000 total points
ID: 16420625
It's easy with regular expressions:

            public static string MakeGoodPath(string path)
                  System.Text.RegularExpressions.Regex r = new System.Text.RegularExpressions.Regex(@"[\\]+",System.Text.RegularExpressions.RegexOptions.IgnoreCase);
                  System.Text.StringBuilder newPath = new System.Text.StringBuilder();
                  foreach(string element in r.Split(path))
                  return newPath.ToString();

Above does not check for drive letter and other things. If you would like to allow also using forward slashes "/" you might need only modify splitting regular expression:
System.Text.RegularExpressions.Regex r = new System.Text.RegularExpressions.Regex(@"[\\]+|/+",System.Text.RegularExpressions.RegexOptions.IgnoreCase);

hope it helps :)

Author Comment

ID: 16424047
Excellent thanks a lot - it works a treat

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses
Course of the Month15 days, 12 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question