C# 无法将文件复制到WPF中的Environment.SpecialFolder.Startup

C# 无法将文件复制到WPF中的Environment.SpecialFolder.Startup,c#,wpf,system,startup,directory,C#,Wpf,System,Startup,Directory,为了让我的应用程序在Windows sturup上启动,我决定设置一个启动文件夹的快捷方式 我尝试使用: File.Move(AppDomain.CurrentDomain.BaseDirectory + "ApplicationName.exe", Environment.GetFolderPath(Environment.SpecialFolder.Startup) + "ApplicationName.lnk"); 它可以工作,但它不会将我的快捷方式移动到我需要的文件夹 Environm

为了让我的应用程序在Windows sturup上启动,我决定设置一个启动文件夹的快捷方式

我尝试使用:

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
只有一个文件夹“落后”

也有“奇怪”的作品。它实际上删除了这个文件,但同样不在“启动”文件夹中

如果我尝试手动将“\Startup”添加到如下路径:

Environment.GetFolderPath(Environment.SpecialFolder.Startup) + @"Startup\ApplicationName.lnk"
我得到了一个System.IO.Exception

我无法手动键入此路径,我需要我的应用程序在具有不同版本Windows的不同PC上工作。我也不能使用注册表使我的应用程序在windows启动时启动

我使用Windows7,VisualStudio2010,.NET4.0,这是一个WPF项目


有什么想法吗?

您是否尝试过Environment.SpecialFolder.CommonStartup而不是Startup,我不知道Startup为什么不能满足您的要求。大多数安装程序包都是为您这样做的;你为什么要为自己做这件事?有没有不使用注册表的原因

我在我的机器上试过这个代码

var startup = Environment.GetFolderPath(Environment.SpecialFolder.CommonStartup);
string file = Path.Combine(startup, "MyApp.lnk");
using (StreamWriter sw = new StreamWriter(file))
{
sw.WriteLine("Test");
}
这是我的创业之旅


您应该使用System.IO.Path.Combine(),这样您就不会创建StartupApplication1.exe。请注意缺少的反斜杠。

请张贴用于创建快捷方式的代码。我从这里开始:CommonStartup也有同样的问题。它将文件复制到C:\ProgramData\Microsoft\Windows\Start Menu\Programs.CommonStartup也有同样的问题。它将文件复制到C:\ProgramData\Microsoft\Windows\Start Menu\Programs。我想手动执行此操作,以便用户有机会关闭和打开此功能。我不想使用注册表,因为对它的访问可能会在不同的用户上被阻止。请经常查看“开始”菜单中的“启动”文件夹,您的代码运行良好。我使用File.Copy()而不是StreamWriter,文件出现在正确的文件夹中。我认为这是关于Path.Combine(),但我不明白它是如何工作的。再次感谢,欢迎。回答:)
var startup = Environment.GetFolderPath(Environment.SpecialFolder.CommonStartup);
string file = Path.Combine(startup, "MyApp.lnk");
using (StreamWriter sw = new StreamWriter(file))
{
sw.WriteLine("Test");
}