How do I find the parent directory in C#?

后端 未结 15 702
旧时难觅i
旧时难觅i 2020-11-29 05:04

I use this code for finding the debug directory

public string str_directory = Environment.CurrentDirectory.ToString();

\"C:\\\\Us

15条回答
  •  臣服心动
    2020-11-29 05:33

    No one has provided a solution that would work cross-form. I know it wasn't specifically asked but I am working in a linux environment where most of the solutions (as at the time I post this) would provide an error.

    Hardcoding path separators (as well as other things) will give an error in anything but Windows systems.

    In my original solution I used:

    char filesep = Path.DirectorySeparatorChar;
    string datapath = $"..{filesep}..{filesep}";
    

    However after seeing some of the answers here I adjusted it to be:

    string datapath = Directory.GetParent(Directory.GetParent(Directory.GetCurrentDirectory()).FullName).FullName; 
    

提交回复
热议问题