簡體   English   中英

在用戶的Startup文件夾中放置一個快捷方式以啟動Windows

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

我想給我的用戶一個“從Windows開始”的選項。 當用戶選中此選項時,它會將快捷方式圖標放入“啟動”文件夾(不在注冊表中)。

在Windows重新啟動時,它將自動加載我的應用程序。

如何才能做到這一點?

您可以使用Enviroment.SpecialFolder枚舉,但根據您的要求,您可能會考慮創建一個Windows服務,而不是一個必須在啟動時啟動的應用程序。

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

編輯:

File.Copy需要原始文件directory-path和目標directory-path來復制文件。 該片段中的關鍵字是Enviroment.GetFolderPath(Enviroment.SpecialFolder.Startup),它將獲取要將文件復制到的啟動文件夾路徑。

你可以用幾種方式使用上面的代碼。 如果你的應用程序有一個安裝程序項目,你可以在安裝時運行這樣的東西。 另一種方式可能是當應用程序啟動時檢查是否存在shorcut,如果不存在則將其放在那里(File.Exists())。

是一個關於在代碼中創建快捷方式的問題。

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



        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM