windows 使用注册表启动程序,并更改当前工作目录?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2822951/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 14:27:22  来源:igfitidea点击:

Use registry to startup a program, and also change the current working directory?

windowsregistry

提问by Joel

I am trying to start a program I made in this directory:

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

C:\example\example.exe -someargument

when the computer starts up. I am attempting to use this registry key:

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

HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run

with the key being:

关键是:

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

But my program also needs files from the directory C:\example but can't find them since the current working directory is different. Is is possible to do something like this in the registry key value

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

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

so that it will change the directory? Or is there a better solution?

这样它会改变目录?或者有更好的解决方案吗?

Thanks!

谢谢!

回答by abatishchev

You can register your application under next registry key (like this does Reg2Run tool)

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

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\App Paths\example.exe

@="c:\example\example.exe"
Path="c:\AnotherPath"

So System.Diagnostics.Run("example.exe");will launch your application with specified working path.

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

Or another way: write a launcher using C#. You can do the same using a PowerShell cmdlet.

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

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

回答by Simon Chadwick

At the start of the application, do the following (this is C#, convert to C++):

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

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

回答by Yuri

You can also create a shortcut for the program in the folder and reference this shortcut in the registry:

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

HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run
Name: example
Type: REG_SZ
Data: "C:\example\example.lnk

回答by Tom A

If the files are always going to be in the same directory as your application, use the Application.ExecutablePathto locate the working directory for the files from within your code, then you can reference them no matter what.

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

回答by Oleg

If you need load DLLs from the same directory you can create subkey example.exeunder

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

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

registry key and define PATHREG_SZvalue example.exe

注册表项和定义PATHREG_SZexample.exe