I need to execute a process in c:\. When I use Process.Processstartinfo it doesn't execute from c:\ and so its dependencies can't be found. It runs in my app's directory instead. How can I run it in the c:\ directory?
Have you set the WorkingDirectory?
process.StartInfo.WorkingDirectory = #"MyWorkingDirectoryPath";
e.g.:
var psi = new ProcessStartInfo();
psi.WorkingDirectory = #"MyWorkingDirectoryPath";
// set additional properties
Process proc = Process.Start(psi);
When the UseShellExecute property is false, gets or sets the working
directory for the process to be started. When UseShellExecute is
true, gets or sets the directory that contains the process to be
started.
Set ProcessStartInfo.WorkingDirectory to the working directory of the process you want to launch, i.e. in your case "C:\".
Related
What is the default directory for WorkingDirectory if it is not defined when using ProcessStartInfo?
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.FileName = #"cscript.exe";
//startInfo.WorkingDirectory = "C:\NotDefined";
The WorkingDirectory, if not set, will be the default %SystemRoot%\system32.
The function of the property depends however on the UseShellExecute flag:
MSDN:
When the UseShellExecute property is false, gets or sets the working directory for the process to be started.
When UseShellExecute is true, gets or sets the directory that contains the process to be started.
You can use the startInfo.WorkingDirectory property to set it.
If the property is not set, the default working directory is %SYSTEMROOT%\system32.
I am trying to execute a batch file server-side in IIS to add a printer using the printuientry call.
The problem I am facing is that I am using the Copy To Output Directory - Copy Always and the following code:
var path = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
var processInfo = new ProcessStartInfo(Path.Combine(path, "AddPrinter.bat"))
{
CreateNoWindow = true,
UseShellExecute = false,
WorkingDirectory = path,
Arguments = ipAddress,
RedirectStandardError = true,
RedirectStandardInput = true,
RedirectStandardOutput = true
};
var process = Process.Start(processInfo);
process.WaitForExit(10000);
process.Close();
Now when deugging, I have checked the values of path and its set to
file:\C:\_Projects\PrinterServerV2\bin
and I have checked to see if the file and directory exist which they do.
But I get the exception:
System.ComponentModel.Win32Exception (0x80004005): The directory name is invalid
Any ideas please??
Check if the user you had set in the iis configuration does have all privileges to run, access, write and read what you wanna do with your batch file.
Also try to change your ProcessStartInfo like the process will be cmd.exe and your batch file the argument.
I had a similar issue How to execute multiples .BAT files in C#
try AppDomain.CurrentDomain.BaseDirectory as path.
I have program pro1.exe that reads from input file, calculates result and writes it to output file.
Now I'm writing program test.exe, that tests it on different tests (fill input, run pro1 using Process.Start() and compares output with supposed)
Problem is following: after executing pro1.exe output file is empty. However, if I run it manually, it writes to output file.
Here is code how I execute pro1:
ProcessStartInfo processInfo = new ProcessStartInfo();
processInfo.FileName = _applicationName;
processInfo.ErrorDialog = true;
processInfo.UseShellExecute = false;
processInfo.RedirectStandardOutput = true;
processInfo.RedirectStandardError = true;
Process proc = Process.Start(processInfo);
_applicationName is a full path to exe file.
in debug I see, that process is starting, and ending without errors.
This is often caused by having a different WorkingDirectory. You likely need to set the WorkingDirectory property to match the executable's path.
Without this, when UseShellExecute == false, the working directory may not be the application's local path.
Hello again Stackoverflow community,
Today I am trying to execute an application with commandline parameters in C#, that not realy difficult like I tried
Process.Start(foldervar + "cocacola.exe", "pepsi.txt");
Cocacola.exe writes and Log in its current folder. In my commandline I write it manually like this
C:\myfolder>cocacola.exe pepsi.txt
Works wonderful but if I try it in C# a total fail.
I read that C# parses the command as C:\myfolder>cocacola pepsi.txt, without the ".EXE" ending. And I tested it manually without the ending, and this does not work.
Now, my question is what is the correct way to get C# executing it C:\myfolder>cocacola.exe pepsi.txt with the ".EXE"
use ProcessStartInfo
http://www.dotnetperls.com/process-start
example:
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.WorkingDirectory=#"c:\someplace";
proc.StartInfo.FileName="cocacola.exe";
proc.StartInfo.Arguments="pepsi.txt";
proc.Start();
proc.WaitForExit();
here is docs on the StartInfo properties:
http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.aspx
Try setting the StartInfo properties.
Process process = new Process();
process.StartInfo.FileName = #"C:\myfolder\cocacola.exe";
process.StartInfo.Arguments = #"C:\myfolder\pepsi.txt";
process.Start();
ProcessStartInfo has the WorkingDirectory property you should set to C:\myfolder
see: http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.workingdirectory.aspx
You need to set the working directory first
string foldervar = #"C:\myfolder";
Process process = new Process();
process.StartInfo.WorkingDirectory = foldervar;
process.StartInfo.FileName = #"cocacola.exe";
process.StartInfo.Arguments = #"pepsi.txt";
process.Start();
Setting the WorkingDirectory is equivilent to cding into the proper directory before running programs. It's what relative paths are relative to.
I'm trying to start an application programatically, but it always runs it in the folder of my application... For example:
If my app is located in C:\MyApp\myapp.exe and the other app is in C:\OtherApp\otherapp.exe, how can I start the other app in the folder in which it resides, rather than in the folder where my app resides?
Here is how I start the other app:
private void StartApp(OtherApp application)
{
Process process = new Process();
process.StartInfo.FileName = application.FileName;
process.StartInfo.Arguments = application.AppName;
process.Start();
}
I guess you mean ProcessStartInfo.WorkingDirectory Property
Use process.StartInfo.WorkingDirectory = pathToTheFolder;.
Just set the WorkDirectory property.
process.StartInfo.WorkingDirectory = Path.GetDirectoryName(application.Filename);