Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在用户';s启动文件夹以Windows启动_C#_.net_Windows_Vb.net - Fatal编程技术网

C# 在用户';s启动文件夹以Windows启动

C# 在用户';s启动文件夹以Windows启动,c#,.net,windows,vb.net,C#,.net,Windows,Vb.net,我想给我的用户一个“从Windows开始”的选项。当用户选中此选项时,它会将快捷方式图标放入启动文件夹(不在注册表中) Windows重新启动时,它将自动加载我的应用程序 如何做到这一点?您可以使用Environment.SpecialFolder枚举,但根据您的要求,您可能会考虑创建windows服务,而不是必须在启动时启动的应用程序 File.Copy("shortcut path...", Environment.GetFolderPath(Environment.SpecialFolde

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

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


如何做到这一点?

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

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

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

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

也是关于在代码中创建快捷方式的问题。

要添加对“IWSRuntimeLibrary”的引用,请在COM下选择“Windows脚本主机对象模型”
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);



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