10

我正在尝试启动我在此目录中制作的程序:

C:\example\example.exe -someargument

当计算机启动时。我正在尝试使用此注册表项:

HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run

关键是:

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

但是我的程序还需要目录 C:\example 中的文件,但由于当前工作目录不同,因此找不到它们。是否可以在注册表项值中执行类似的操作

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

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

谢谢!

4

5 回答 5

12

您可以在下一个注册表项下注册您的应用程序(就像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);
于 2010-05-12T21:47:40.997 回答
5

在应用程序启动时,执行以下操作(这是 C#,转换为 C++):

    using System.IO;
:
:
    Environment.CurrentDirectory = Path.GetDirectoryName(Application.ExecutablePath);
于 2010-05-12T21:56:14.133 回答
3

您还可以在文件夹中为程序创建快捷方式,并在注册表中引用此快捷方式:

HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run
Name: example
Type: REG_SZ
Data: "C:\example\example.lnk
于 2019-01-25T07:52:28.417 回答
0

如果文件始终与您的应用程序位于同一目录中,请使用Application.ExecutablePath从代码中找到文件的工作目录,然后无论如何您都可以引用它们。

于 2010-05-12T21:46:21.873 回答
0

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

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

注册表项和定义PATH REG_SZexample.exe

于 2010-05-12T21:51:21.313 回答