【问题标题】:Moving up a folder from the executable从可执行文件上移文件夹
【发布时间】:2017-04-19 08:16:44
【问题描述】:

我正在尝试在可执行文件上方的文件夹中创建一个文件路径。例如,我希望变量TAGPATH 成为文件夹C:\User\ApplicationFolder\tag_rw\tag_rw.exe 中可执行文件的文件路径,而应用程序位于C:\User\ApplicationFolder\AppFiles 中。我希望应用程序是可移植的,这意味着无论文件夹名称如何,它都会检索应用程序可执行文件的文件路径,然后转到父文件夹并导航到 tag_rw\tag_rw.exe

我基本上想要string TAGPATH = @"path_to_appfolder\\tag_rw\\tag_rw.exe"

这是我到目前为止已经厌倦的(使用第一个答案How to navigate a few folders up?):

  string appPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
  string TAGPATH = System.IO.Path.GetFullPath(System.IO.Path.Combine(appPath, @"..\"));

我收到一个运行时错误ArgumentException,描述为URI formats are not supported.

有没有更简单/更好的方法来解决这个问题?

谢谢!

【问题讨论】:

  • 如果我没有误解你的问题,你只是想得到父目录
  • 是的。应该只是说,而不是长段。获取初始父目录的父目录也很好。

标签: c# directory


【解决方案1】:

你可以试试这个吗?

    static void Main(string[] args)
    {
        string cur = Environment.CurrentDirectory;
        Console.WriteLine(cur);
        string parent1 = Path.Combine(cur, @"..\");
        Console.WriteLine(new DirectoryInfo(parent1).FullName);
        string parent2 = Path.Combine(cur, @"..\..\");
        Console.WriteLine(new DirectoryInfo(parent2).FullName);
        Console.ReadLine();
    }

【讨论】:

  • 完美运行。谢谢。
  • 很高兴听到这个消息!
【解决方案2】:

导航仅限于绝对和相对类型。我认为您的意思是导航到父目录,而不管整个应用程序的位置如何。 也许你试试相对路径

string TAGPATH = "..\\tag_rw\\tagrw.exe"

【讨论】:

    猜你喜欢
    • 2010-10-03
    • 2015-07-28
    • 1970-01-01
    • 1970-01-01
    • 2014-06-02
    • 2021-09-13
    • 1970-01-01
    • 2016-12-08
    • 1970-01-01
    相关资源
    最近更新 更多