为了使我的应用程序在Windows sturup上启动,我决定将一个快捷方式放到Startup文件夹中。
我试图使用:
File.Move(AppDomain.CurrentDomain.BaseDirectory + "ApplicationName.exe", Environment.GetFolderPath(Environment.SpecialFolder.Startup) + "ApplicationName.lnk");
它有效,但它将我的快捷方式移动到我需要的文件夹。
Environment.GetFolderPath(Environment.SpecialFolder.Startup)
效果很好,它会返回:
C:\Users\Germanov\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup
但我的快捷方式显示在
中C:\Users\Germanov\AppData\Roaming\Microsoft\Windows\Start Menu\Programs
只有1个文件夹“后面”。
File.Delete(Environment.GetFolderPath(Environment.SpecialFolder.Startup) + "ApplicationName.lnk");
也有效“奇怪”。它实际上删除了这个文件,但是再次没有删除“Startup”文件夹。
如果我尝试手动将“\ Startup”添加到这样的路径:
Environment.GetFolderPath(Environment.SpecialFolder.Startup) + @"Startup\ApplicationName.lnk"
我得到一个System.IO.Excseption。
我无法手动输入此路径,我的应用程序在具有不同版本Windows的不同PC上工作。我也无法使用Registry使我的应用程序从Windows启动开始。
我使用Windows 7,Visual Studio 2010,.NET 4.0,这是一个WPF项目。
有什么想法吗?
答案 0 :(得分:3)
您是否尝试过Environment.SpecialFolder.CommonStartup而不是Startup,我不知道为什么启动不能满足您的要求。大多数安装程序包都是为您执行此操作;为什么你想为自己做这件事?有没有使用注册表的原因?
我在我的机器上试过这段代码
var startup = Environment.GetFolderPath(Environment.SpecialFolder.CommonStartup);
string file = Path.Combine(startup, "MyApp.lnk");
using (StreamWriter sw = new StreamWriter(file))
{
sw.WriteLine("Test");
}
它来自我的创业公司
答案 1 :(得分:2)
您应该使用System.IO.Path.Combine(),这样就不会创建StartupApplication1.exe。请注意缺少的反斜杠。