C# .NET normal to verbatim string conversion

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rhitam30111985
    New Member
    • Aug 2007
    • 112

    C# .NET normal to verbatim string conversion

    Hi all ... i have a tricky problem.. cant seem to find a solution to it :

    [CODE=cs]

    using System;
    using System.Collecti ons.Generic;
    using System.Text;
    using System.Text.Reg ularExpressions ;

    namespace ConsoleApplicat ion1


    {
    class myfunc
    {
    public string GetFileName(str ing szPath)
    {
    Regex r = new Regex(@"\w+[.]\w+$+");
    return r.Match(szPath) .Value;
    }
    }

    class Program
    {
    static void Main(string[] args)
    {
    myfunc a = new myfunc();
    string path = "c:\tet\tee\tet e.txt";
    string final_path = '@' + path;
    Console.WriteLi ne(final_path);
    Console.WriteLi ne(a.GetFileNam e(final_path));
    Console.ReadLin e();
    }
    }
    }

    [/CODE]

    the above code gives me folllowing line of output :

    @C: et ee ete.txt
    ete.txt

    what i am trying to do is convert the normal string to verbatim string so that the escape sequences are ignored and the correct file name is outputted ... is this possible ?

    ie .. the output shud be :

    c:\tet\tee\tete .txt
    tete.txt
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    use:
    Code:
    string path = @"c:\tet\tee\tete.txt";
    System.IO.FileInfo fi = new System.IO.FileInfo(path);
    Console.WriteLine(path );
    Console.WriteLine(fi.Name);
    Console.ReadLine();

    Comment

    • rhitam30111985
      New Member
      • Aug 2007
      • 112

      #3
      No i cant do that becoz i i am using this in a form where the user selects a file using the filedialog class ... the string returned is a normal string .. for further processing i need to convert it .. .

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        The string returned from a filedialog will be correctly escaped and can be used as is.

        Also note that I edited my above post.


        And an edit here for filedialog usage:
        Code:
        string fullpath = myOpenFileDialog.FileName;
        System.IO.FileInfo fi = new System.IO.FileInfo(fullpath);
        Console.WriteLine(fullpath);
        Console.WriteLine(fi.Name);
        Console.ReadLine();

        Comment

        • rhitam30111985
          New Member
          • Aug 2007
          • 112

          #5
          oh.. ok ... . will try that ... thanks ..

          Comment

          Working...