c#自动运行问题

本文关键字:问题 运行 | 更新日期: 2023-09-27 18:01:29

我设置c#应用程序在启动时通过注册表自动运行。问题是:

Environment.CurrentDirectory //returns C:'Windows'System32

而不是文件的实际位置。为什么会造成这种情况,有没有其他的选择?

我使用'HKEY_LOCAL_MACHINE'SOFTWARE'Microsoft'Windows'Current Version'Run'

c#自动运行问题

CurrentDirectory不是您的程序的位置;这是程序启动的文件夹。(虽然可以稍后更改)

您要找的是Path.GetDirectory(typeof(YourType).Assembly.Location)

我不知道问题是什么,但您可以随时更改当前目录为您的应用程序,如下所示:

Environment.CurrentDirectory = AppDomain.CurrentDomain.BaseDirectory;

试试System.Windows.Forms.Application.StartupPath

当我在c#中创建windows启动时的自动运行时,我使用下一个指令来获取我的可执行路径:

System.Windows.Forms.Application.ExecutablePath.ToString();