Check Path Format

Posted on 2006-04-09
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

    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(@"\") ? @"\" : "";
    LVL 6

    Accepted Solution

    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

    Excellent thanks a lot - it works a treat

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
    Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…

    730 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

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now