【发布时间】:2010-08-02 21:28:42
【问题描述】:
我想为我的用户提供“从 Windows 开始”选项。当用户选中此选项时,它会将快捷方式图标放入启动文件夹(不在注册表中)。
在 Windows 重新启动时,它会自动加载我的应用程序。
如何做到这一点?
【问题讨论】:
我想为我的用户提供“从 Windows 开始”选项。当用户选中此选项时,它会将快捷方式图标放入启动文件夹(不在注册表中)。
在 Windows 重新启动时,它会自动加载我的应用程序。
如何做到这一点?
【问题讨论】:
您可以使用 Enviroment.SpecialFolder 枚举,但根据您的要求,您可能会考虑创建 Windows 服务而不是必须在启动时启动的应用程序。
File.Copy("shortcut path...", Environment.GetFolderPath(Environment.SpecialFolder.Startup) + shorcutname);
编辑:
File.Copy 需要一个原始文件目录路径和目标目录路径来复制文件。该 sn-p 中的关键是 Enviroment.GetFolderPath(Enviroment.SpecialFolder.Startup) ,它获取您要将文件复制到的启动文件夹路径。
您可以通过多种方式使用上述代码。如果您的应用程序有安装程序项目,您可以在安装时运行类似的内容。另一种方法可能是当应用程序启动时,它会检查快捷方式是否存在,如果不存在则将其放在那里(File.Exists())。
Here 也是一个关于在代码中创建快捷方式的问题。
【讨论】:
WshShell wshShell = new WshShell();
IWshRuntimeLibrary.IWshShortcut shortcut;
string startUpFolderPath =
Environment.GetFolderPath(Environment.SpecialFolder.Startup);
// Create the shortcut
shortcut =
(IWshRuntimeLibrary.IWshShortcut)wshShell.CreateShortcut(
startUpFolderPath + "\\" +
Application.ProductName + ".lnk");
shortcut.TargetPath = Application.ExecutablePath;
shortcut.WorkingDirectory = Application.StartupPath;
shortcut.Description = "Launch My Application";
// shortcut.IconLocation = Application.StartupPath + @"\App.ico";
shortcut.Save();
【讨论】:
private void button2_Click(object sender, EventArgs e)
{
string pas = Application.StartupPath;
string sourcePath = pas;
string destinationPath = @"C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup";
string sourceFileName = "filename.txt";//eny tipe of file
string sourceFile = System.IO.Path.Combine(sourcePath, sourceFileName);
string destinationFile = System.IO.Path.Combine(destinationPath);
if (!System.IO.Directory.Exists(destinationPath))
{
System.IO.Directory.CreateDirectory(destinationPath);
}
System.IO.File.Copy(sourceFile, destinationFile, true);
}
【讨论】: