I am developing a C# windows form application project. The form contains a button and after the button click event cmd.exe is started using Process.Start()
method. The StartInfo.Arguments()
contains path to an exe file stored in the project resources folder (I have created a new folder called Resources and another subfolder called SW1. The exe file is inside SW1 folder)
Right now, the exe file is stored in a different location in local computer and I use the full path to refernce the exe file in StartInfo.Argument(). However, when i publish the C# application, I want the exe file will be bundled as a resource. How can I reference the exe file in this case?
private async void button1_Click(object sender, EventArgs e)
{
await RunCMDAsync();
}
private async static Task RunCMDAsync()
{
try
{
using (Process myProcess = new Process())
{
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "cmd.exe";
startInfo.Arguments = @"/C cd <path to the exe file on my local computer here> & <name of exe --additional arguments>";
startInfo.UseShellExecute = false;
startInfo.RedirectStandardInput = true;
myProcess.StartInfo = startInfo;
myProcess.Start();
myProcess.WaitForExit();
}
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
}
In the above code, Whenever the cmd.exe process is started, I want to pass two arguments using StartInfo.Arguments()
startInfo.Arguments = @"/C cd <path to the exe file on my local computer here> & <name of exe --additional arguments>";
The first argument is to cd into the folder which contains the exe file ( /C cd <path to the exe file on my local computer here>
)
The second argument runs the exe file with some parameters ( <name of exe --additional arguments>
)
How can I reference the exe file in Resources > SW1 folder after I publish this as a standalone application? Do I need to extract the exe file to a location on the target computer during installing the C# application and use the same code as above or is there a way to directly reference resources folder?
If what you need is to find the directory where your program is running (I'm not sure I understood your problem) then use System.AppDomain.CurrentDomain.BaseDirectory
.
string exePath = System.AppDomain.CurrentDomain.BaseDirectory +
"Resources\\SW1\\" + exefilenameWithExtension;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.