Default directory when using WorkingDirectory when using ProcessStartInfo - c#

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.

Related

Working a process in background

I am using C# to run another .exe applicaiton.
however I want to run the .exe in background
I tried Start info hide bu.t it didn't help
How can I do that?
currently this is the code I am using
_p = new System.Diagnostics.Process();
_startInfro = new System.Diagnostics.ProcessStartInfo();
_startInfro.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
_startInfro.FileName = tmp[0];
_startInfro.Arguments = deleteRangeCommand;
_p.StartInfo = _startInfro;
_p.Start();
Since I you don't provide information about which .exe you want to start I can't be sure about this answer. But the CreateNoWindow property looks like what you need.
You must also set the UseShellExecute property to false as is pointed out in the documentation. Otherwise the value of CreateNoWindow is ignored.

Executing batch file in WCF

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.

how to hide windows host script c#.net

I need hide hide Windows host script after this code:
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "/C slmgr.vbs /dlv";
process.StartInfo = startInfo;
process.Start();
Does anyone know how I could do this?
If you are talking about just hiding the window completely then you are missing setting the CreateNoWindow property.
startInfo.CreateNoWindow = true;
Instead of firing up an additional command-prompt, use cscript.exe to execute your VB script. If you do it this way you won't have to worry about the shell figuring out how to execute you vbs file and you won't get an additional command-line window.
startInfo.FileName = "cscript.exe";
startInfo.Arguments = "slmgr.vbs /dlv";
If the executable that is creating this process is not in the same directory as slmgr.vbs, you'll also need to set the full path to the file in the arguments, or set the working directory that the process runs in.
// Example path where your scripts could reside.
startInfo.WorkingDirectory = #"C:\PathToMyScripts\VBScripts\";
You probably want to redirect the output as well so you can log it somewhere.
Thank You for mr.BrutalDev
the second step
change startInfo.Arguments = "slmgr.vbs /dlv";
to startInfoent.Arguments = "C:\\Windows\\System32\\slmgr.vbs /dlv";

Executing a Process in a Specific Directory

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:\".

Commandline Arguments in C#

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.

Categories

Resources