【问题标题】:How to get current working directory path c#?如何获取当前工作目录路径 c#?
【发布时间】:2014-03-10 16:04:17
【问题描述】:

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

F:/r.cur  

问题是这是硬编码路径而且我想要相对路径,这样如果我将我的解决方案移动到另一个系统,代码应该不会生效。

请建议如何设置相对路径

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

【问题讨论】:

  • 放入资源文件夹,引用为Properties.Reources.xxx
  • 您是否尝试将 r.cur 打包到您的项目目录中?
  • @David 是的,我已经尝试过将 r.cur 文件放在资源文件夹中,但它没有在代码中显示。它只显示 .jpg 类型的文件
  • @hbsrud 如果我尝试将 r.cur 打包在同一个项目目录中,我仍然需要提供绝对路径。这似乎没问题。但如果我设置项目并将其发送给另一个用户,它仍然可能具有依赖性
  • @yogeshkmrsoni01,您是否右键单击资源文件并将操作设置为“嵌入资源”,而不是“无”或“编译”?

标签: c# .net visual-studio-2010 c#-4.0


【解决方案1】:

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

System.IO.Directory.GetCurrentDirectory();

所以你可以使用以下获取应用程序可执行文件的目录路径:

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

【讨论】:

  • 上述代码在 WPF 中不起作用,因为它没有 System.Windows.Forms 命名空间。改用以下 System.IO.Path.GetDirectoryName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName)
【解决方案2】:

使用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,你能建议任何其他方式
  • 请注意:这仅适用于 WinForms
【解决方案3】:

您也可以通过
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);

输出:

获取文件名:                                     Mouse.aspx
GetFileNameWithoutExtension:           鼠标
GetDirectoryName:                             E:\abccom\Cat

快乐编码:)

【讨论】:

    【解决方案4】:

    参加这个聚会超级迟到,但是当我进行单元测试时,这对我有用。

    var currentDirectory = Directory.GetCurrentDirectory();
    

    如果你需要项目的根目录,而不是 bin 目录,那么这个:

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

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

    【讨论】:

      【解决方案5】:

      您可以使用System.IO.Directory.GetCurrentDirectory() 获取当前工作目录。它会返回你当前的可执行路径。

      谢谢

      【讨论】:

        【解决方案6】:

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

        【讨论】:

          【解决方案7】:

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

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

          【讨论】:

            【解决方案8】:

            怎么样:Environment.CurrentDirectory

            查看更多信息:https://docs.microsoft.com/en-us/dotnet/api/system.environment?view=net-5.0

            【讨论】:

              猜你喜欢
              • 2019-02-18
              • 2012-04-12
              • 1970-01-01
              • 1970-01-01
              • 2010-12-28
              • 1970-01-01
              • 2019-01-27
              • 1970-01-01
              • 2014-04-22
              相关资源
              最近更新 更多