Run File on Server from WPF Button Press - c#

Some application code using Process.Start() and PsExec.exe to start a video on a serverpc is running OK when run from a Console application, but not when run from a button press in a WPF application. This sent me nuts today.
So:
I am running a small WPF app on a PC which, once a button is pressed, will send a command to a server PC to run a video file. I am using PsExec.exe to run the process on the server interactively (did not manage with WMI)
This is the code I'm using:
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.FileName = #"C:\Windows\System32\PsExec.exe";
p.StartInfo.Arguments = #"\\192.168.1.3 -u Administrator -p hagarmikejessav -i cmd.exe /c START E:\Media\FerroniConcettaAapp\Videos\Photoslideshow.mp4";
p.StartInfo.CreateNoWindow = true;
p.Start();
No, this exact same code can open video file Photoslideshow.mp4 on the server PC (192.168.1.3) when run from a normal console App. However when I try to run it after pressing a button in a WPF app, p.Start() gives me a "The system cannot find the file specified" error. Here is the WPF code snippet (it's the same as above):
private void Video1_btn_Click(object sender, RoutedEventArgs e)
{
try
{
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.FileName = #"C:\Windows\System32\PsExec.exe";
p.StartInfo.Arguments = #"\\192.168.1.3 -u Administrator -p hagarmikejessav -i cmd.exe /c START E:\Media\FerroniConcettaAapp\Videos\Photoslideshow.mp4 //fullscreen";
p.StartInfo.CreateNoWindow = true;
p.Start();
}
}
When I tried to use p.Start on a file that is local to my PC, that opened as expected. It's just the server that is not 'seeing' the FileName. As I said originally, the same code only fails on p.Start when accessing it after a button click.
What am I doing wrong? Please someone tell me that this is the result of hours in front of the PC and that it's only a stupid mistake which I cannot see!.
EDIT:
After more playing around,I realised that the error "The system cannot find the file specified" related to this line:
p.StartInfo.FileName = #"C:\Windows\System32\PsExec.exe";
enter image description here
Upon changing this line to:
p.StartInfo.FileName =#"C:\Windows\System32\Notepad.exe";
and removing the next line, Notepad opens up on my local PC. However, when I change the 2 lines back to something like:
p.StartInfo.FileName = #"Notepad.exe";
p.StartInfo.Arguments = #"\192.168.1.3 -u Administrator -p pass-i cmd.exe /c START C:\realtek.txt";...
notepad opens on my local PC but an error "Network path not found". (which is a similar error to when I run the 'non-button- code.)
Thus I know the problem is something to do with either the WPF/Button application. But I dont know what the problem is!
thanks a lot,
Mario

I managed to figure it out.
For some reason, the system was not finding PSExec from this path (even though it existed in that folder).
p.StartInfo.FileName = #"C:\Windows\System32\PsExec.exe";
Upon copying the File to another directory and using the full path, it finally worked.

Related

.exe file behaves differently when run from Unity to when open manually

I am attempting to run a .exe from Unity. The .exe runs perfectly when I open it manually by double-clicking, but from Unity, it just opens then does not work at all.
The .exe is a very basic python script (that I made into an executable) that reads a text file and then creates another one. When run from Unity the executable window says that this file does not exist/can't be found, when I know it does, and then immediately closes.
I have tried running this .exe with these methods:
Application.OpenURL(path);
And:
Process.Start(path);
The .exe works perfectly fine when I click on it and has no dependencies or anything other than that one text file.
How can I run this file from code as if it had just been clicked?
process = new Process();
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.FileName = path;
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
process.StartInfo.ErrorDialog = false;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.EnableRaisingEvents = true;
process.Start();
But I think that your path is invalid. Can you Debug.Log it and write in the comment what it is? It should be absolute path btw
The solution is to define the process' directory like this:
Process p = new Process();
p.StartInfo.FileName = path + "app.exe";
p.StartInfo.WorkingDirectory = path;
p.Start();
with path being the path to the folder where the app.exe is located.

Running a Curl Command from C#

I'm trying to run a curl command from a C# program. My code is below. When I run the code below, I get an exception that the file is not found. I want to be able to do this but I do not want to use a batch file as a parameter for the filename. That is because the arguments for my curl command are variable based upon other conditions in the C# code. My variable strCmdText has the arguments for the curl command (the source and destination files). There are other examples of this on Stackoverflow, but they all use a batch file which I'm trying to avoid.
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.FileName = "C:\\Windows\\System32\\curl.exe";
p.StartInfo.Arguments = strCmdText;
p.StartInfo.UseShellExecute = false;
p.Start();
p.WaitForExit();
I changed my code to the following:
System.Diagnostics.ProcessStartInfo p = new
System.Diagnostics.ProcessStartInfo();
p.UseShellExecute = true;
p.WorkingDirectory = "C:\\Windows\\System32\\";
p.FileName = "curl.exe";
p.ErrorDialog = true;
p.CreateNoWindow = true;
System.Diagnostics.Process.Start(p);
From a DOS prompt, curl does exist in this directory. But I still get the curl not found message.
Something has to be strange with the path here. When I put a break point in though, and view the Environment class, System32 is in the path.
Curl is available at the location: C:\Windows\System32\curl.exe
That only leaves the source file to be the culprit of a "File not found" issue.
As you're launching curl through a process, ensure that your paths are escaped properly in your startup arguments.
Alternatively, you could launch curl through cmd (through a process), you can try with the following, changing the command-line arguments from --help to suit your desired action.
string script = $"\"C:\\Windows\\System32\\curl.exe\" --help";
Process process = new Process()
{
StartInfo = new ProcessStartInfo()
{
FileName = "cmd",
Arguments = script
}
};
process.Start();
Please note that this is in principle, essentially using a batch file as it's just throwing some commands into a cmd.
I had the exact same problem. Just delete curl.exe from System32 and place it on another folder (dont't forget the dependences, dlls, etc.).
Then in the line
p.StartInfo.FileName = "C:\\Windows\\System32\\curl.exe";
Overwrite "C:\\Windows\\System32\\curl.exe" to "C:\\NEW PATH\\curl.exe".
Note: You MUST delete it from System32. If you just copy to the new location it will still don't work.

How to combine multiple gz files into one from Process in C# program when one is missing EOF

I have multiple .gz files in a directory (2 or more), with at least one file missing the end of file marker. Our C# process is unable to read the file with missing end of file, but since they are coming from a third party we do not have control over how they are created.
As such, we've been running the following Linux command manually:
cat file1.gz file2.gz > newFile.gz
In order to automate this, I am looking for a way to leverage the Process functionality in C# to trigger the same command, but this would only be available in Cygwin or some other Linux shell. In my example, I'm using git bash but it could be Powershell or Cygwin or any other available Linux shell that runs on a Windows box.
The following code does not fail, but it does not work as expected. I am wondering if anyone has recommendations about how to do this or any suggestions on a different approach to consider?
Assume that the working directory is set and initialized successfully, so the files exist where the process is run from.
Process bashProcess = new Process();
bashProcess.StartInfo.FileName = #"..\Programs\Git\git-bash.exe";
bashProcess.StartInfo.UseShellExecute = false;
bashProcess.StartInfo.RedirectStandardInput = true;
bashProcess.StartInfo.RedirectStandardOutput = true;
bashProcess.Start();
bashProcess.StandardInput.WriteLine("cat file1.gz file2.gz > newFile.gz");
bashProcess.StandardInput.WriteLine("exit");
bashProcess.StandardInput.Flush();
.
.
.
bashProcess.WaitForExit();
My expectation is that newFile.gz is created
I was able to find a solution to my problem using a DOS command, and spawning a cmd Process from CSharp.
My code now looks like this, avoids having to launch a linux-based shell from Windows, and the copy command in windows does the same thing as cat:
Process proc = new Process();
proc.EnableRaisingEvents = false;
proc.StartInfo.FileName = "cmd";
proc.StartInfo.Arguments = #"/C pushd \\server\folder && copy *.txt.gz /b
combined.gz";
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.RedirectStandardError = true;
proc.StartInfo.UseShellExecute = false;
proc.Start();
proc.WaitForExit();
System.Threading.Thread.Sleep(2000);
string line = proc.StandardOutput.ReadLine();
while (line != null)
{
output.Append(line);
line = proc.StandardOutput.ReadLine();
}

Process.Start runs on Windows 8 but crashes on Windows 7

So I'm using the following code to perform a process using cmd.exe. It works fine on Windows 8 (my dev machine). However, when the executable is run from a Windows 7 computer, instead of getting the black pop up box with print out, its just a black box that flashes really fast and disappears with no error. There also seems to be nothing in the title of the cmd window except cmd.exe.
StandardOutput when the window closes, is blank.
wgetLocation is C:\Users\Kevin M\Desktop\wget\wget.exe
Also, when trying to catch the exception, there wasn't one.
Any help is appreciated.
//Make Folder to Store files
string downloadedFiles = Path.Combine(Directory.GetCurrentDirectory(), txt_Site.Text);
Directory.CreateDirectory(downloadedFiles);
//Get List of Sites
//Directory.SetCurrentDirectory(wgetLocation);
wgetLocation = Path.Combine(wgetLocation, "wget.exe");
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = "/K " + wgetLocation + " --spider -r --adjust-extension -A html -A aspx " + txt_Site.Text;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.Start();
Console.Write(p.StandardOutput.ReadToEnd());
p.WaitForExit();
Working:
Not Working:

Permission issues when running JScript from C# Console application

I'm trying to run a Jscript task from a C# console application.
The Jscipt file is not mine so I can't change it. The script moves some files and this is what is causing the issues.
When I run the script manually, i.e. form the shell it executes correctly. When I try and run the script from my console application the bulk of the process runs but I get a ":Error = Permission denied" error when it tries to move the files.
I've tried every permutation of the Diagnostics.Process class that I can think of but I've had no luck.
My current code:
Process process = new Process();
process.StartInfo.WorkingDirectory = Path.GetDirectoryName((string)path);
process.StartInfo.FileName = #"cmd.exe";
process.StartInfo.Arguments = "/C " + (string)path;
process.StartInfo.UseShellExecute = false;
process.StartInfo.Verb = "runas";
process.StartInfo.LoadUserProfile = true;
process.StartInfo.Domain = "admin";
process.StartInfo.UserName = #"cardax_sync_test";
process.StartInfo.Password = GetSecureString("abc123");
process.Start();
process.WaitForExit();
Any ideas?
Thanx
Rookie Mistake!
I forgot to close the text reader that creates one of the input files for the jscript.
I'll submit this question for deletion when it get's old enough. Don't want more useless info clogging up the net!

Categories

Resources