Home >Backend Development >C++ >How Can I Set the Default Directory for Launched Processes in C# .NET?
When launching an external process (such as a Java application) in a C# .NET console application, you may need to specify the default directory where the process runs. This is especially important for processes that depend on supporting files in the current directory.
In some cases, a Java application launched from a .NET process cannot find necessary support files because its default working directory is different from the current directory of the calling process.
Is there a way to specify a default directory to use when launching a process in .NET?
Yes, the ProcessStartInfo
class provides a property called WorkingDirectory
that allows you to set the default directory for starting processes. By using this property, you can ensure that the process can access the required files.
<code class="language-csharp">using System.Diagnostics; ... var startInfo = new ProcessStartInfo(); startInfo.WorkingDirectory = @"C:\MyProject\JavaApp"; // 设置工作目录 // 设置其他属性 Process proc = Process.Start(startInfo);</code>
This code sets the default directory for the proc process to the specified path, ensuring that any necessary supporting files present in that directory are accessible to the process.
The above is the detailed content of How Can I Set the Default Directory for Launched Processes in C# .NET?. For more information, please follow other related articles on the PHP Chinese website!