Java 如何将文件添加到启动文件夹?

Java 如何将文件添加到启动文件夹?,java,c#,xcode,visual-studio,Java,C#,Xcode,Visual Studio,我正在尝试使按钮1\u单击将calc.exe添加到启动文件夹。我还想让计算器在屏幕上随机弹出 我正在使用Visual Studio 2015。 如果还使用java,我可以使用什么代码在单击按钮时将文件添加到启动文件夹。我试着用启动文件夹的链接创建这个文件,但我一直出错。找不到文件,即使该文件已存在于“项目”文件夹中 using System; using System.Collections.Generic; using System.ComponentModel; using System.D

我正在尝试使
按钮1\u单击
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);

        }
    }
}

以下是您可以使用的代码:

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

下面的代码应该可以解决您的问题,它还将打开计算器

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