Skip to main content

 none
Should a directory path variable end with a trailing slash? RRS feed

  • Question

  • Dear All,

    What is the difference between "c:\test" and "c:\test\"?

    1. Should the directory path variable be end with a trailing slash?

    2. When do we need to add the trailing slash?

    Thanks and Best regards,

    E-John

    Tuesday, September 10, 2019 6:31 AM

All replies

  • Tuesday, September 10, 2019 6:38 AM
  • Hi E-John,

    Thank you for posting here.

    >>What is the difference between "c:\test" and "c:\test\"?

    If it is used for accessing the path, there is no difference between them.

    >> Should the directory path variable be end with a trailing slash?

    As usual, we don't need to add it.

    >>When do we need to add the trailing slash?

    If we want to use it to access the file in that folder, you will use it. Please refer to the following code.

                string path1 = @"c:\test";
                string path2 = @"c:\test\";
                Console.WriteLine(path1);
                Console.WriteLine(path2);
                string newpath = Path.Combine(path1, "new.txt"); // first way 
                Console.WriteLine(newpath);
                string newpath1 = path2 + "new.txt";          // second way
                Console.WriteLine(newpath1);

    Result:

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, September 10, 2019 7:41 AM
    Moderator
  • Hi

    Is your problem solved? If so, please post "Mark as answer" to the appropriate answer. So that it will help other members to find the solution quickly if they face the similar issue.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, September 16, 2019 9:27 AM
    Moderator
  • Hi Jack,

    Thanks for your explanations.

    Path.GetDirectoryName(path) got different result if path with/without trailing slash

    if path = "D:\Test\Dir1\" ---> got directory name = "D:\Test\Dir1\"

    ---> If we regards path with a trailing slash is a directory, then it is expectation.

    if path = "D:\Test\Dir1" ---> got directory name = "D:\Test"

    ---> Here the "D:\Test\Dir1" is not end with a slash, but "D:\Test\" which ends with slash is a directory.

    Method Path.GetDirectoryName(path) got directory is "D:\Test\"

    It is better to add trailing slash to represent it is a directory.


    D:.
    │  TestFile1.txt

    └─Dir1
            TestDir1File1.txt

    namespace DirsAndFiles
    {
        class Program
        {
            static void Main(string[] args)
            {
                string path = @"D:\Test\Dir1\";
                string folder = Path.GetDirectoryName(path);
                var directory = new DirectoryInfo(folder);
    
                var latestFile = (from f in directory.GetFiles("*.txt")
                                  orderby f.LastWriteTime descending
                                  select f).First();
                Console.WriteLine("Dirctory with slash");
                Console.WriteLine(path);
                Console.WriteLine(folder);
                Console.WriteLine(directory);
                Console.WriteLine(latestFile.Name);
    
                path = @"D:\Test\Dir1";
                folder = Path.GetDirectoryName(path);
                directory = new DirectoryInfo(folder);
    
                latestFile = (from f in directory.GetFiles("*.txt")
                                  orderby f.LastWriteTime descending
                                  select f).First();
                Console.WriteLine(new string('=', 40));
                Console.WriteLine("Dirctory without slash");
                Console.WriteLine(path);
                Console.WriteLine(folder);
                Console.WriteLine(directory);
                Console.WriteLine(latestFile.Name);
    
                Console.ReadKey();
            }
        }
    }



    • Edited by E-John Thursday, September 19, 2019 3:37 AM
    Thursday, September 19, 2019 3:36 AM
  • Hi E-John,

    Thanks for the feedback.

    I want to know if you have any other question about it. If so, please feel free to let us know. If not, you could mark the useful reply as an answer.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, September 19, 2019 5:50 AM
    Moderator