当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - Get folder path from local machine to execute process of other application on button click of windows application

问题描述:

How to give specific folder path to start process using C#.Net code?

I have written following code to start the my another application exe(i.e. "ConsoleApplication1.exe") which is stored at (D:) drive of PC.

for (int i = 0; i < chkListBox.CheckedItems.Count; i++)

{

if ((string)chkListBox.Items[i] == "ConsoleApplication1")

{

string path = @"D:\TAS\Development\Latest\ConsoleApplication1\ConsoleApplication1\bin\Debug";

string files = Directory.GetDirectoryRoot(path);

var process = new Process

{

StartInfo = new ProcessStartInfo

{

FileName = path+chkListBox.Items[i].ToString()+".exe"

}

};

process.Start();

}

}

When Clicked on Button then I got following exception:

Exception:



网友答案:

There are quite a few potential problems with the code you posted. It's hard to know what your current problem is without seeing the exception, but I think this is the most likely issue:

On the line FileName = path+chkListBox.Items[i].ToString()+".exe", you appear to be missing a path separator.

Use Path.Combine instead of manual string concatenation to combine paths. By using Path.Combine, you eliminate a whole category of mistakes that can be made when combining two strings to make a new path.

var process = new Process
{
    StartInfo = new ProcessStartInfo
    {
       FileName = Path.Combine(path, chkListBox.Items[i].ToString()+".exe");
    }
};
分享给朋友:
您可能感兴趣的文章:
随机阅读: