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

标签 c# wpf system startup directory

为了让我的应用程序在 Windows 上启动,我决定在启动文件夹中添加一个快捷方式。

我尝试使用:

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 上运行。我也无法使用注册表使我的应用程序随 Windows 启动而启动。

我使用 Windows 7、Visual Studio 2010、.NET 4.0,这是一个 WPF 项目。

有什么想法吗?

最佳答案

您是否尝试过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");
}

它即将在我的初创公司推出

enter image description here

关于c# - 无法将文件复制到 WPF 中的 Environment.SpecialFolder.Startup,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6789497/

相关文章:

c# - NullReferenceException 与 resharper 建议的对象初始值设定项

c# - 为什么@User.IsInRole在_Layout.cshtml中总是返回false

c# - 如何使用必需的非空列设置 filehelpers

c# - 使用不安全的方法比较两个图像数据

c# - INotifyPropertyChanged - 数据访问、业务或 UI 层

c++ - 系统 ("command") 字符串中的字符串问题

java - 获取 Action 监听器中的系统时间

c# - 提高 C# 循环的效率

WPF 双值数据绑定(bind)与自定义小数点分隔符

perl - 为什么 system() 即使它执行的程序死了也返回 0