2016-04-15 128 views
0

我試圖將button1_Click原因calc.exe添加到啓動文件夾中。我也想讓計算器在屏幕上隨機彈出。如何將文件添加到啓動文件夾?

我正在使用Visual Studio 2015. 如果還使用java我可以使用什麼代碼將文件添加到按鈕單擊的啓動文件夾。我試圖創建與啓動文件夾的鏈接文件,但我一直在獲取錯誤。文件未找到,即使該文件是在項目文件夾

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 

using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 

using System.Text; 
using Microsoft.Win32; 
using System.Windows.Forms; 

namespace WindowsFormsApplication1 
{ 
    public partial class Hey : Form 
    { 
     public Hey() 
     { 
      InitializeComponent(); 
     } 


     private void Form1_Load(object sender, EventArgs e) 
     { 

     } 

     private void label1_Click(object sender, EventArgs e) 
     { 

     } 

     private void button2_Click(object sender, EventArgs e) 
     { 

     } 

     private void button1_Click(object sender, EventArgs e) 
     { 

      System.Diagnostics.Process.Start("calc.exe"); 
      RegistryKey Key = Registry.CurrentUser.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Run", true); 
      Key.SetValue("calc.exe", System.Reflection.Assembly.GetEntryAssembly().Location); 

     } 
    } 
} 

回答

0

這裏已存在的是,你可以使用代碼:

File.Copy(PathToFile, Environment.GetFolderPath(Environment.SpecialFolder.Startup)); 

或使用File.Move如果你不想複製您的應用程序:

File.Move(PathToFile, Environment.GetFolderPath(Environment.SpecialFolder.Startup)); 

如果你想使你的應用程序啓動隨機使用此代碼:

Thread ithr = new Thread(() => 
{ 
    Random rnd = new Random(); 
    while(true) 
    { 
     Thread.Sleep(rnd.Next(10000, 60000) //1000 = 1sec. | random start 10s. - 60s. 
     Process.Start(PathToFile); 
    } 
}); 
ithr.Start(); 
0

下面的代碼應該適合您的問題,它也會打開計算器。

private void button1_Click(object sender, EventArgs e) 
    { 
     var pathToCalculator = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), "calc.exe"); 

     var copyOfCalcInStartup= Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Startup), "calc.exe"); 

     File.Copy(pathToCalculator, copyOfCalcInStartup); 

     Process.Start(pathToCalculator); 
    } 
相關問題