locked
Regex to replace a drive letter RRS feed

  • Question

  • Hello

    I've got the path L:\Program Files\folder1\file.txt

    I need to replace the L: drive to C:  or what ever drive letter it may be to C:

    How do I do that with Regular expressions?

    Thanks

    • Moved by Lisa Zhu Wednesday, June 19, 2013 2:10 AM Regular Expressions related
    Monday, June 17, 2013 6:39 PM

Answers

  • Regex.Replace(@"L:\test\path.exe", "^.", "C")

    Monday, June 17, 2013 7:22 PM
  • Alternate to Regex..

                string path = @"L:\Program Files\folder1\file.txt";
                path = "C:\\" + path.Substring(System.IO.Path.GetPathRoot(path).Length); 
                Console.WriteLine(path);
    

    • Proposed as answer by Pantelis44999 Wednesday, June 19, 2013 5:40 AM
    • Marked as answer by Mike Feng Monday, June 24, 2013 2:23 PM
    Monday, June 17, 2013 7:47 PM

All replies

  • Regex.Replace(@"L:\test\path.exe", "^.", "C")

    Monday, June 17, 2013 7:22 PM
  • Alternate to Regex..

                string path = @"L:\Program Files\folder1\file.txt";
                path = "C:\\" + path.Substring(System.IO.Path.GetPathRoot(path).Length); 
                Console.WriteLine(path);
    

    • Proposed as answer by Pantelis44999 Wednesday, June 19, 2013 5:40 AM
    • Marked as answer by Mike Feng Monday, June 24, 2013 2:23 PM
    Monday, June 17, 2013 7:47 PM
  • Hi Villiam256,

    From the description, I will move this thread to the most related forum for better support.

    Thanks for your understanding.

    Regards,


    Lisa Zhu [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, June 19, 2013 2:07 AM