C#从本地目录打开程序

Ivo*_*man 1 c#

我对C#几乎没有经验,但我非常愿意学习.我正在尝试使用启动可执行文件的按钮创建应用程序.该应用程序从USB闪存驱动器运行.让我们说闪存驱动器在我的电脑上有driveletter(e :).我想从bin目录运行一个名为rkill.exe的程序.

private void opschonen_RKill_Click(object sender, EventArgs e)
    {
        var process_RKill = new Process
        {
            StartInfo = new ProcessStartInfo
            {
                FileName = "/bin/rkill.exe"
            }
        };
        process_RKill.Start();
        process_RKill.WaitForExit();
    }
Run Code Online (Sandbox Code Playgroud)

但是,这不起作用.如果我从根启动应用程序,它确实有效.我不能指向一个驱动器,因为不是每台计算机都将驱动器分配给E:

我究竟做错了什么?我想这很简单,因为我只是一个初学者.

Emi*_*els 5

const string relativePath = "bin/rkill.exe";

//Check for idle, removable drives
var drives = DriveInfo.GetDrives()
                      .Where(drive => drive.IsReady
                             && drive.DriveType == DriveType.Removable);

foreach (var drive in drives)
{
    //Get the full filename for the application
    var rootDir = drive.RootDirectory.FullName;
    var fileName = Path.Combine(rootDir, relativePath);

    //If it does not exist, skip this drive
    if (!File.Exists(fileName)) continue;

    //Execute the application and wait for it to exit
    var process = new Process
    {
        StartInfo = new ProcessStartInfo
        {
            FileName = fileName
        }
    };

    process.Start();
    process.WaitForExit();
}
Run Code Online (Sandbox Code Playgroud)