CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Oct 2004
    Location
    Rocket City
    Posts
    220

    Question Bug in GetFolderPath (beta)?

    I am using Visual Studio 2005 (and .NET Framework), build beta2.50215-4400, with Microsoft Windows XP Professional, v5.1.2600 Service Pack 2 Build 2600.

    When I use the following statement, the result has DOUBLED backslashes in every position which, of course, do not work very well in Windows IO functions.
    Code:
            string AStr = System.Environment.
                            GetFolderPath(
                                System.Environment.SpecialFolder.
                                    ApplicationData).ToString();
    Locals:

    AStr "C:\\Documents and Settings\\user.login\\Application Data"
    Has anyone else encountered this anomaly?

  2. #2
    Join Date
    May 2004
    Location
    Greenville, NC
    Posts
    193

    Re: Bug in GetFolderPath (beta)?

    That's strange. When I run it my output is

    Quote Originally Posted by Output
    C:\Documents and Settings\Jason\Application Data
    Press any key to continue . . .
    Quote Originally Posted by SystemInfo
    OS Name Microsoft Windows XP Professional
    Version 5.1.2600 Service Pack 2 Build 2600
    OS Manufacturer Microsoft Corporation
    The code is exactly as you have it.

    Code:
    static void Main(string[] args)
    {
        string AStr = System.Environment.
                    GetFolderPath(
                        System.Environment.SpecialFolder.
                            ApplicationData).ToString();
        Console.WriteLine(AStr);
    }
    Jason Isom
    .NET 2.0 / VS2005 Developer

  3. #3
    Join Date
    Sep 1999
    Location
    Madurai , TamilNadu , INDIA
    Posts
    1,024

    Re: Bug in GetFolderPath (beta)?

    Do you see "@" infront of the value when you see it in "Local window"? If not, the extra slash is probably an escape char. Like Jason did, you can check the value by printing them in the console.

  4. #4
    Join Date
    Aug 2005
    Location
    Seattle, Wa
    Posts
    179

    Re: Bug in GetFolderPath (beta)?

    it's an escape char. You cannot have a \ in a string without escaping it first.

    string tmp = "this \will fail";

    You can escape it by using a double backslash, or by specifying the string with an @ sign to contain no escape codes.

    string tmp = "this \\will not fail";
    string tmp = @"this \will not fail";

    the output of:

    string path = "C:\\program files\\someprogram\\";

    can be used properly and displayed properly as:

    "C:\program files\someprogram\"
    Last edited by crackersixx; October 7th, 2005 at 01:32 PM.

  5. #5
    Join Date
    Oct 2004
    Location
    Rocket City
    Posts
    220

    Re: Bug in GetFolderPath (beta)?

    I have found that
    Code:
    Path.DirectorySeparatorChar.ToString() == "\\"
    on this system, contrary to what is stated in help topic
    ms-help://MS.VSCC.v80/MS.MSDNQTR.v80.en/MS.MSDN.v80/MS.NETDEVFX.v20.en/cpref/html/F_System_IO_Path_DirectorySeparatorChar.htm
    Thus, Path.Combine also puts DOUBLE backslash between the two arguments.

    BTW, this DOUBLE backslash did not occur using the same statements on my previous machine Win2000, using VS v1.1.
    Last edited by zips; October 7th, 2005 at 01:37 PM.

  6. #6
    Join Date
    May 2004
    Location
    Greenville, NC
    Posts
    193

    Re: Bug in GetFolderPath (beta)?

    Quote Originally Posted by zips
    I have found that
    Code:
    Path.DirectorySeparatorChar.ToString() == "\\"
    I'm confused. What's your point? The following is also true.

    Code:
    Path.DirectorySeparatorChar.ToString() == @"\"
    There is no double slash.
    Last edited by Jason Isom; October 7th, 2005 at 01:38 PM.
    Jason Isom
    .NET 2.0 / VS2005 Developer

  7. #7
    Join Date
    Oct 2004
    Location
    Rocket City
    Posts
    220

    Re: Bug in GetFolderPath (beta)?

    The point is that folder and filenames built up using the System.(etal) are not being understood by, for example, OpenFileDialog InitialDirectory or FileName with WinXP VS2005, whereas the same code worked fine with Win2000 VS2003.
    Last edited by zips; October 7th, 2005 at 02:00 PM.

  8. #8
    Join Date
    Sep 1999
    Location
    Madurai , TamilNadu , INDIA
    Posts
    1,024

    Re: Bug in GetFolderPath (beta)?

    I tested your code in VS2005 Beta 2 and Windows XP professional SP2. It works fine. The problem you face is somewhere else.

    Post a small compilable sample code to reproduce your problem (including the OpenFileDialog code).

  9. #9
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Bug in GetFolderPath (beta)?

    Quote Originally Posted by zips
    I have found that
    Code:
    Path.DirectorySeparatorChar.ToString() == "\\"
    And that is correct.
    Quote Originally Posted by zips
    on this system, contrary to what is stated in help topic
    No, it's exactly what MSDN says:
    The value of this field is a slash ("/") on Unix, and a backslash ("\") on the Windows and Macintosh operating systems.
    backslash (\) is an escape caracter. So, if you want to have a string with a backslash in it you must escape it like this "\\". In C# you can use @ before the string to create a verbatim string (what you see is what you get), in which case you don't need escaping the backslash. @"\"

    The correct reading of MSDN is:
    The value of this field is a slash (/) on Unix, and a backslash (\) on the Windows and Macintosh operating systems.
    (notice the missing quotes)
    Run this code and see what it outputs:
    Code:
    Console.WriteLine("\\");
    Console.WriteLine(@"\");
    Then try to compile this one:
    Code:
    Console.WriteLine("\");
    You will see that the string is not ended, because the second " is escaped with the backslash.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured