Check Path Format

Hi

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

Thanks

wbcintsol
wbcintsolAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fahad MukhtarDistinguished EngineerCommented:
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(@"\") ? @"\" : "";
0
VovinECommented:
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))
                        if(element!=String.Empty)
                              newPath.Append(element).Append(@"\\");
                  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 :)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
wbcintsolAuthor Commented:
Excellent thanks a lot - it works a treat
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.