c# ProcessStartInfo - c#

Why is this
ProcessStartInfo myProcess = new ProcessStartInfo(path);
myProcess.UserName = username;
myProcess.Password = MakeSecureString(password);
myProcess.UseShellExecute = false;
Process.Start(myProcess);
working, but
ProcessStartInfo myProcess = new ProcessStartInfo();
myProcess.FileName = Path.GetFileName(path);
myProcess.WorkingDirectory = Path.GetDirectoryName(path);
myProcess.UserName = username;
myProcess.Password = MakeSecureString(password);
myProcess.UseShellExecute = false;
Process.Start(myProcess);
is not.
I wanted to use the second one because of this question: https://stackoverflow.com/a/2621943/1306186
I am constantly getting a file not found exception... Any ideas?
Edit:
Path is for example #"C:\Users\User\Desktop\ConsoleApplication2.exe"

This bit is wrong
myProcess.FileName = Path.GetFileName(path);
this should be
myProcess.FileName = path;
Pass in C:\SomeDir\SomeApp.exe and the code you have will set the filename to SomeApp.exe, which it can't find. Count yourself lucky, there are circumstances where it could have (e.g. your app and the app you want to run being in the same folder), and then you would have possibly got a funny when deploying.

I would try using Path.GetFullPath() instead of simply Path.GetFileName() since the constructor initializes the FileName with the full path when you use it with the string parameter.

Related

"The system cannot find the file specified" error on process.Start();

I am trying to get the process respond as a string so I can use it in different place in my code, this is the solution that I have so far:
const string ex1 = #"C:\Projects\MyProgram.exe ";
const string ex2 = #"C:\Projects\ProgramXmlConfig.xml";
Process process = new Process();
process.StartInfo.WorkingDirectory = #"C:\Projects";
process.StartInfo.FileName = "MyProgram.exe ";
process.StartInfo.Arguments = ex2;
process.StartInfo.Password = new System.Security.SecureString();
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
try
{
process.Start();
StreamReader reader = process.StandardOutput;
string output = reader.ReadToEnd();
}
catch (Exception exception)
{
AddComment(exception.ToString());
}
But when I'm running this I get:
"The system cannot find the file specified" error in process.Start(); without
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
The code runs fine but it just open console window and all the process response is trow there so I can't use it as string.
Does anyone know why I am getting this error or maybe a different solution to my problem?
I suspect the problem is that the filename you're specifying is relative to your working directory, and you're expecting Process.Start to look there when starting the process - I don't believe it works that way when UseShellExecute is false. Try just specifying the absolute filename of the process you want to start:
process.StartInfo.FileName = #"C:\Projects\MyProgram.exe";
Note that I've also removed the space from the end of the string you were assigning for the FileName property - it's entirely possible that was casuing the problem too.
For System32 access if you are trying to RUN an x86 Application on x64 then you must use the "Sysnative" keyword instead of "System32" in your filename.
EG: instead of:
C:\Windows\System32\whoiscl.exe
It should be:
C:\Windows\Sysnative\whoiscl.exe
Hope this helps someone

Command Syntax for Processstartinfo from variables

Can someone please help me out with the syntax here
string sourcePath = #textBox2.Text.ToString();
string targetPath = #textBox1.Text.ToString();
System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo(#"XCOPY C:\folder D:\Backup\folder /i");
psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
psi.UseShellExecute = false;
System.Diagnostics.Process copyFolders = System.Diagnostics.Process.Start(psi);
copyFolders.WaitForExit();
I am trying to change this line below to replace c:\folder with (sourcePath) and D:\Backup\folder with targetPath
(#"XCOPY C:\folder D:\Backup\folder /i");
I keep getting source not found when I messagebox like this it looks ok
MessageBox.Show(#"XCOPY " + (sourcePath) + (targetPath) + " /i");
You'll need to debug, I'd start here:
string args = string.format("{0} {1} /i", sourcePath, targetPath);
Debug.WriteLine(args);
//verify your paths (both) are correct
string cmd = string.format("XCOPY {0}", args);
Debug.WriteLine(cmd);
//copy the command and test it out directly in cmd
Also, try passing your arguments....as arguments....
System.Diagnostics.ProcessStartInfo psi =
new System.Diagnostics.ProcessStartInfo("XCOPY", args);
You can look at the documentation for an example of passing with arguments
Nothing there really looks wrong. Perhaps debug it so you can get the text out of it and copy/paste it to Explorer to make sure the paths are valid and there's nothing funky going on?
Also, you don't need to do .ToString() on the .Text property of a TextBox. it's already a string.

C# Process.Start() - target app can't find file

I'm trying to start application "GA.exe", but on start it's taking data from file "acc.txt".
If I start it normallly (via double click :-)) it works, but if I use code below it say "Can't find acc.txt".
My first idea:
Process.Start(pathToGA.exe);
Second idea:
ProcessStartInfo pinfo = new ProcessStartInfo()
{
Arguments = FolderWithGA.exePath,
FileName = pathToGA.exe,
};
And both don't work.
You should set ProcessStartInfo.WorkingDirectory to the directory that holds acc.txt and GA.exe:
ProcessStartInfo pinfo = new ProcessStartInfo()
{
Arguments = FolderWithGA.exePath,
FileName = pathToGA.exe,
WorkingDirectory = FolderWithGA
};

Directory.GetCurrentDirectory

I've written a program that checks if it is in a specific folder;
if not, it copies itself into that folder,run the copied program and exit.
but the problem is when I call
Directory.GetCurrentDirectory();
in the copied program(only when It runs by the first one) I get the directory of the first program not the copied one.
What's the problem here?
the code:
if(Directory.GetCurrentDirectory()!=dir)
{
File.Copy(Application.ExecutablePath,dir+name);
System.Diagnostics.Process.Start(dir+#"\"+name);
System.Environment.Exit(System.Environment.ExitCode);
}
i summarized my codes.
You have to use the WorkingDirectory on the processinfo, no need for copying the files.
if(Directory.GetCurrentDirectory()!=dir)
{
string exepath = Path.Combine(dir,name);
ProcessStartInfo processStartInfo = new ProcessStartInfo();
process.StartInfo.FileName = exepath;
processStartInfo.WorkingDirectory = dir;
//Set your other process info properties
Process process = Process.Start(processStartInfo);
System.Environment.Exit(System.Environment.ExitCode);
}

.NET: How to pass command-line parameters with a temp file name

This is .NET 2.0 WinForms. I have some code like so
string str = Path.GetTempFileName();
Process p = new Process();
ProcessStartInfo psi = new ProcessStartInfo();
psi.Arguments = str
psi.FileName = <some executable >
p.StartInfo = psi;
p.Start();
Now on the "started" process I get the temp file name by saying args[0]. On Win XP this is causing an issue as the temp file is in C:\Documents and Settings\....
The space is causing an issue, thus args[0] is C:\Documents.
How can I fix this? Do I just have to place str in quotes? Or can I get the whitespace to be ignored somehow?
Yes, use quotes.
You can either wrap the path in double quotes or you can convert the path into its short (8.3) representation using the native GetShortPathName function.

Categories