How to get current working directory path c#?

后端 未结 7 1014
清歌不尽
清歌不尽 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.

    0 讨论(0)
  • 2020-12-15 15:37

    System.IO.Path.GetDirectoryName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName) at startup will give you the full path.

    After that, if you * really want to find something during development (as your comments in other answers point) *, first find the path using FileInfo(thestringwithfilenamepath).Directory.Name.

    0 讨论(0)
  • 2020-12-15 15:39

    You can get the current working directory by using System.IO.Directory.GetCurrentDirectory(). it will return your current executable path.

    Thanks

    0 讨论(0)
  • 2020-12-15 15:40

    use Application.StartupPath returns path for the executable file that started the application.

            string pathCur = Path.Combine(Application.StartupPath, @"..\..\r.cur");
            Cursor = new Cursor(pathCur);
    
    0 讨论(0)
  • 2020-12-15 15:44

    You can use static Directory class - however current directory is distinct from the original directory, which is the one from which the process was started.

    System.IO.Directory.GetCurrentDirectory();
    

    So you can use the following to get the directory path of the application executable:

    System.IO.Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath);
    
    0 讨论(0)
  • 2020-12-15 15:47

    Application.StartupPath should give you application path from where your application is running. I would create a directory structure under application folder. e.g If "C:\Program Files\MyApp" is my application folder, then I would create a folder named cursors under it (C:\Program Files\MyApp\Cursors") and put all cursors within this folder.

    0 讨论(0)
提交回复
热议问题