如何在C#中获取当前工作目录路径?

58

我在项目中有一个光标文件。我已经在代码中给出了绝对路径,即

F:/r.cur  

问题在于这是硬编码路径,我想要相对路径,以便如果我将我的解决方案移动到另一个系统,代码就不会受到影响。

请建议如何设置相对路径。

//current code i am using 
 p.Cursor = new Cursor("F:/r.cur");

4
将其放入资源文件夹,并将其作为 Properties.Resources.xxx 引用。 - David
你尝试过将 r.cur 打包到你的项目目录中吗? - hbsrud
1
@David 是的,我已经尝试过将r.cur文件放在资源文件夹中,但它没有显示在代码中。它只显示.jpg类型的文件。 - yogeshkmrsoni01
1
@ David,我学了一件新事物。但它还没有显示出来..;) 你有其他建议吗? - yogeshkmrsoni01
@hbsrud 如果我这样做,新路径将是:C:/..../MyProj/bin/Release/Cursor/r.cur;但我想要的是 C:/..../MyProj/Cursor/r.cur; - yogeshkmrsoni01
显示剩余6条评论
10个回答

79
你可以使用静态的Directory类,但是当前目录与原始目录不同,原始目录是指启动该进程时的那个目录。

你可以使用静态的Directory类,但当前目录与原始目录不同,原始目录是指启动进程时的目录。

System.IO.Directory.GetCurrentDirectory();

所以,您可以使用以下内容来获取应用程序可执行文件的目录路径:

System.IO.Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath);

16
以上代码在WPF中不起作用,因为它没有System.Windows.Forms名称空间。请改用以下代码:System.IO.Path.GetDirectoryName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName)。 - Avinash Jain

4

你也可以使用
System.IO.Directory.GetCurrentDirectory();
但是它会显示bin和debug文件夹,如果你不想显示这些文件夹,可以使用以下代码:

string page = "E:\abccom\Cat\Mouse.aspx"

string name = Path.GetFileName(page );
string nameKey = Path.GetFileNameWithoutExtension(page );
string directory = Path.GetDirectoryName(page );

Console.WriteLine("{0}, {1}, {2}, {3}",
page, name, nameKey, directory);

输出结果:

GetFileName:                                        Mouse.aspx
GetFileNameWithoutExtension:           Mouse
GetDirectoryName:                              E:\abccom\Cat

愉快的编码 :)


4

使用Application.StartupPath方法可以获取启动应用程序的可执行文件路径。

        string pathCur = Path.Combine(Application.StartupPath, @"..\..\r.cur");
        Cursor = new Cursor(pathCur);

它返回的是C:\Users\yogeshkmrsoni\Documents\Visual Studio 2010\Projects\ProjectName\bin\Release路径;但我想将光标文件复制到C:\Users\yogeshkmrsoni\Documents\Visual Studio 2010\Projects\ProjectName\r.cur,你能提供其他方法吗? - yogeshkmrsoni01
1
请注意:这仅适用于WinForms。 - sonyisda1

3

1

来晚了,但这取决于你的意思 - 如果你想知道你的应用程序代码最终部署到哪个目录(例如在你的测试机器上),那么对于常规控制台应用程序,可以使用以下方法:

//gives something like D:\source\repos\Repo\src\AppName\bin\Debug\net6.0\Appame.dll
var assemblyLocation = Assembly.GetEntryAssembly().Location; 

//gives something like D:\source\repos\Repo\src\AppName\bin\Debug\net6.0
var currentDirectory = Path.GetDirectoryName(assemblyLocation);

//gives something like D:\source\repos\Repo\src\AppName
var appRootFolder = currentDirectory.Split("\\bin")[0];

Console.WriteLine($"The root of the application is here: {appRootFolder}");

0

System.IO.Path.GetDirectoryName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName) 在启动时将会给你完整的路径。

之后,如果你*真的想要在开发过程中找到一些东西(正如你在其他答案中的评论所指出的) *,首先使用FileInfo(thestringwithfilenamepath).Directory.Name找到路径。


0

你可以通过使用System.IO.Directory.GetCurrentDirectory()来获取当前工作目录。它将返回你当前的可执行文件路径。

谢谢


0

Application.StartupPath 应该会给你应用程序运行的路径。我会在应用程序文件夹下创建一个目录结构。例如,如果 "C:\Program Files\MyApp" 是我的应用程序文件夹,则我会在其中创建一个名为 cursors 的文件夹 ("C:\Program Files\MyApp\Cursors"),并将所有光标放入此文件夹中。


0

对于 .NET 6 和 .NET 7,我使用:

var filePath = Path.Combine(AppContext.BaseDirectory, $"someFile.txt");

0

虽然我来晚了,但这对于我在单元测试中的工作非常有效。

var currentDirectory = Directory.GetCurrentDirectory();

如果你需要项目的根目录,而不是 bin 目录,则这样:

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

如果你在网站上,情况会有所不同。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接