在用户的启动文件夹中放置一个快捷方式以启动 Windows

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3391923/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 14:57:21  来源:igfitidea点击:

placing a shortcut in user's Startup folder to start with Windows

c#.netwindowsvb.net

提问by jameslcs

I wanted to give my user an option for "Start with Windows". When user check this option it will place a shortcut icon into Startup folder (not in registry).

我想给我的用户一个“从 Windows 开始”的选项。当用户选中此选项时,它将在启动文件夹中放置一个快捷方式图标(不在注册表中)。

On Windows restart, it will load my app automatically.

在 Windows 重新启动时,它会自动加载我的应用程序。

How can this be done?

如何才能做到这一点?

回答by Patrick Kafka

you can use the Enviroment.SpecialFolder enum, although depending on your requirements you might look at creating a windows service instead of an app that has to start on startup.

您可以使用 Enviroment.SpecialFolder 枚举,但根据您的要求,您可能会考虑创建 Windows 服务而不是必须在启动时启动的应用程序。

File.Copy("shortcut path...", Environment.GetFolderPath(Environment.SpecialFolder.Startup) + shorcutname);

edit:

编辑:

File.Copy needs an origin file directory-path and the target directory-path to copy a file. The key in that snippet is Enviroment.GetFolderPath(Enviroment.SpecialFolder.Startup) which is getting the startup folder path where you want to copy your file to.

File.Copy 需要一个原始文件目录路径和目标目录路径来复制文件。该代码段中的关键是 Enviroment.GetFolderPath(Enviroment.SpecialFolder.Startup),它获取要将文件复制到的启动文件夹路径。

you could use the above code several ways. In case you've got an installer project for your app, you could run something like this on install. Another way could be when the app launches it checks if the shorcut exists there and puts one there if not (File.Exists()).

您可以通过多种方式使用上述代码。如果您的应用程序有一个安装程序项目,您可以在安装时运行类似的程序。另一种方法可能是当应用程序启动时,它会检查快捷方式是否存在,如果不存在,则将其放在那里(File.Exists())。

Hereis a question about creating shortcuts in code also.

这里还有一个关于在代码中创建快捷方式的问题。

回答by Waruna Manjula

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();

回答by ????? ????

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);



        }