启动位置与应用程序的位置不同。

时间:2012-01-06 14:58:33

标签: c#

我需要在Windows启动时启动我的c#应用程序。我试图通过使用以下两个选项添加注册表项与应用程序的exe路径。

System.Environment.CurrentDirectory
Directory.GetCurrentDirectory()

但是当Windows启动时,文件夹路径显示到system32目录而不是应用程序的目录。因此,依赖于当前目录的代码无法正常工作。如何解决这个实现到程序文件夹?我搜索了相关帖子,但没有获得解决方案。

谢谢,


此代码适用于我:

Directory.GetParent(System.Windows.Forms.Application.ExecutablePath)

这给出了放置应用程序的目录。

非常感谢。

5 个答案:

答案 0 :(得分:1)

我认为你需要在这里使用exe路径而不是当前目录。查看 System.Reflection.Assembly.GetExecutingAssembly ,然后 Assembly.Location

答案 1 :(得分:1)

var myDir = AppDomain.CurrentDomain.BaseDirectory;

AppDomain.CurrentDomain.BaseDirectory on MSDN

答案 2 :(得分:1)

您可以从当前程序集的位置检索路径:

var assembly = System.Reflection.Assembly.GetEntryAssembly();
var curDir = System.IO.Path.GetDirectoryName(assembly.Location);

答案 3 :(得分:0)

你可以尝试

System.Reflection.Assembly.GetExecutingAssembly().Location

答案 4 :(得分:0)

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

希望这有帮助!