2016-12-05 79 views
0

我正在尝试在可执行文件之上的文件夹内创建一个文件路径。例如,我希望将变量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.

有没有更容易/更好的方法去做这件事?

谢谢!

+0

如果我没有误解你的问题,你只是想获得**父目录**? –

+0

是的。应该说,而不是长段。获得初始父目录的父目录也很好。 – NM24

回答

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(); 
    } 
+0

完美地工作。谢谢。 – NM24

+0

很高兴听到! –

1

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

string TAGPATH = "..\\tag_rw\\tagrw.exe" 
相关问题