使用registry来启动一个程序,也改变当前的工作目录?

我试图启动一个我在这个目录下的程序:

C:\example\example.exe -someargument 

当电脑启动时。 我正在尝试使用此registry项:

 HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run 

关键是:

 Name: example Type: REG_SZ Data: "C:\example\example.exe -someargument" 

但是我的程序也需要C:\ example目录中的文件,但是由于当前的工作目录不同,所以找不到它们。 有可能在registry键值中做这样的事情

 "cd C:\example\; example.exe -someargument" 

这样它会改变目录? 还是有更好的解决scheme?

谢谢!

您可以在下一个注册表项下注册您的应用程序(就像Reg2Run工具一样 )

 HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\App Paths\example.exe @="c:\example\example.exe" Path="c:\AnotherPath" 

所以System.Diagnostics.Run("example.exe"); 将以指定的工作路径启动您的应用程序。

或者另一种方法:使用C#编写一个启动器。 您可以使用PowerShell cmdlet执行相同的操作。

 var info = new System.Diagnostics.ProcessStartInfo(@"c:\example\example.exe", "-someargument") { WorkingDirectory = @"c:\AnotherPath" }; System.Diagnostics.Process.Start(info); 

在应用程序的开始,执行以下操作(这是C#,转换为C ++):

  using System.IO; : : Environment.CurrentDirectory = Path.GetDirectoryName(Application.ExecutablePath); 

如果这些文件总是和你的应用程序在同一个目录中,那么使用Application.ExecutablePath来定位代码中文件的工作目录,然后你可以引用它们。

如果你需要从同一目录加载DLL,你可以在下面创建子键example.exe

 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths 

注册表项并定义PATH REG_SZexample.exe