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

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.

Related

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();
}

Run File on Server from WPF Button Press

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.

running a JScript file in cmd.exe by a button

I have a button in my form that when its clicked, it will open command prompt and automatically run a javascript file. My code so far only opens the command prompt. How do you run a javascript file?
Process process = new Process();
process.StartInfo.FileName = #"C:\windows\system32\cmd.exe";
process.Start();
Now that we know that you're trying to launch a Node.js app, try this new version.
And don't forget that C# is pretty well documented on MSDN.
If the rest of your code is working as is, and you don't actually want/need to run CMD.exe, this should do the trick:
Process process = new Process();
process.StartInfo.FileName = #"C:\Program Files\nodejs\node.exe";
process.StartInfo.Arguments = #"P:\ath\To\Your\File.js";
process.Start();
As a holdover from the original question: If you had been trying to launch a JScript script, you would want to use #"C:\Windows\System32\Cscript.exe", or if you want to launch a JScript script without seeing a command prompt window, replace Cscript with Wscript.
Try this:
process.StartInfo.FileName = #"C:\windows\system32\cmd.exe";
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.UseShellExecute = false;
process.Start();
process.StandardInput.WriteLine("Cscript.exe \"PathToYourFile\\file.js\"");
//OR
//process.StandardInput.WriteLine("Wscript.exe \"PathToYourFile\\file.js\"");
process.StandardInput.Flush();
process.StandardInput.Close();

Windows defrag is not recognized as an internal or external command, operable program or batch file

I'm working on making a tech-toolkit program, and included in this 'toolkit' will be a button which runs a defrag on the local disk. Currently the batch file I've made for this is simple, it just runs a basic fragmentation analysis:
defrag C: /A
The C# code behind the button that triggers this is:
System.Diagnostics.ProcessStartInfo procInfo =
new System.Diagnostics.ProcessStartInfo();
procInfo.Verb = "runas";
procInfo.FileName = "(My Disk):\\PreDefrag.bat";
System.Diagnostics.Process.Start(procInfo);
This code does exactly what I want, it calls UAC then launches my batch file with Administative Privledges. Though once the batch file is ran, the output I recieve to the command console is:
C:\Windows\system32>defrag C: /A
'defrag' is not recognized as an internal or external command,
operable program or batch file.
What causes this Error and how do I fix it?
Check to make sure your defrag.exe file actually exists in C:\Windows\System32.
Try fully qualifying the 'defrag' command as:
C:\WINDOWS\system32\defrag.exe C: /A
Open up a cmd prompt and run this command: defrag.exe /? and post in the question what you get.
First of all: set yout project Platform Target property to Any CPU and untick the Prefer 32-bit option (99,9% this is the issue). Then... why starting a batch that invokes the command when you can just do this?
ProcessStartInfo info = new ProcessStartInfo();
info.Arguments = "/C defrag C: /A";
info.FileName = "cmd.exe";
info.UseShellExecute = false;
info.Verb = "runas";
info.WindowStyle = ProcessWindowStyle.Hidden;
Process.Start(info);
Works like a charm on my machine. For multiple commands:
ProcessStartInfo info = new ProcessStartInfo();
info.FileName = "cmd.exe";
info.RedirectStandardInput = true;
info.UseShellExecute = false;
Process cmd = Process.Start(info);
using (StreamWriter sw = p.StandardInput)
{
if (sw.BaseStream.CanWrite)
{
sw.WriteLine(command1);
sw.WriteLine(command2);
// ...

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