简体   繁体   English

C#Directory.GetCurrentDirectory和C以外的驱动器:

[英]C# Directory.GetCurrentDirectory and drives other than C:

For some reason when my program is installed on a drive other than C:\\ the code below (c# .net 2.0) is unable to find and run the program2.exe. 由于某些原因,当我的程序安装在C:\\以外的驱动器上时,下面的代码(c#.net 2.0)无法找到并运行program2.exe。 Am I doing something wrong here? 我在这里做错什么了吗?

try
{
    System.Diagnostics.Process proc = new System.Diagnostics.Process();
    proc.StartInfo.FileName = Directory.GetCurrentDirectory() +
                              "\\Folder\\program2.exe";
    proc.Start();
}
catch
{
    MessageBox.Show("Unable to locate program");
}

Could it be because your program is in the root folder of another drive, eg R:\\ , rather than a subdirectory, eg R:\\Program ? 可能是因为您的程序位于另一个驱动器的根文件夹中,例如R:\\ ,而不是子目录例如R:\\Program

For reasons such as this, it is considered bad practice in C# to concatenate paths using the literal backslash character. 由于诸如此类的原因,在C#中使用文字反斜杠字符连接路径被认为是不好的做法。 Instead, you should use Path.Combine : 相反,您应该使用Path.Combine

proc.StartInfo.FileName = Path.Combine(
    Directory.GetCurrentDirectory(),
    "Folder",
    "program2.exe"
);

Furthermore, it may be that Directory.GetCurrentDirectory() does not point to the directory you think it does. 此外,可能Directory.GetCurrentDirectory()没有指向您认为的目录。 In general, this could return any directory on your system, and is unrelated to where your program resides or was launched from. 通常,这可以返回系统上的任何目录,并且与程序所在的位置或启动程序的位置无关。 Therefore, I recommend to use one of these instead: 因此,我建议改用以下方法之一:

  • If you are using WinForms, Application.ExecutablePath 如果您使用的是WinForms,则Application.ExecutablePath
  • Otherwise, Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) 否则, Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#Directory.GetCurrentDirectory()返回带有NUnit的system32 - C# Directory.GetCurrentDirectory() returning system32 with NUnit C#:从浏览器启动时的Directory.getCurrentDirectory() - C#: Directory.getCurrentDirectory() when launching from Browser Directory.GetCurrentDirectory() 方法在不同类型的 C# 项目中给出不同的路径 - Directory.GetCurrentDirectory() method gives different path in different type of C# project Directory.GetCurrentDirectory - Directory.GetCurrentDirectory System.CurrentDomain.AppDomain.BaseDirectory和Directory.GetCurrentDirectory()之间的C#差异 - C#-Difference between System.CurrentDomain.AppDomain.BaseDirectory and Directory.GetCurrentDirectory() Directory.GetCurrentDirectory()不能在linux上运行? - Directory.GetCurrentDirectory() not working on linux? 将转义序列添加到Directory.GetCurrentDirectory() - adding escape sequence to Directory.GetCurrentDirectory() WPF设置中的默认值为“ Directory.GetCurrentDirectory”。 - Default value in WPF Settings as `Directory.GetCurrentDirectory` 无法将文件写入 Directory.GetCurrentDirectory() 中的路径 - Unable to write file to path in Directory.GetCurrentDirectory() Directory.GetCurrentDirectory()根据命令行参数返回不同的结果 - Directory.GetCurrentDirectory() returns different results based on command line arguments
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM