How to get current working directory path c#?

后端 未结 7 1052
清歌不尽
清歌不尽 2020-12-15 15:08

I have a cursor file in project. I have given the absolute path in code i.e

F:/r.cur  

the problem is this is hard-coded path And i Want

7条回答
  •  半阙折子戏
    2020-12-15 15:34

    Super late to this party, but this works for me when I'm in unit tests.

    var currentDirectory = Directory.GetCurrentDirectory();
    

    If you need the root of the project, and not the bin directory then this:

    var currentDirectory = Directory.GetCurrentDirectory();
    var basePath = currentDirectory.Split(new string[] { "\\bin" }, StringSplitOptions.None)[0];
    

    It'll be different if you're on a website.

提交回复
热议问题