Run a command line process from unity - c#

I am trying to run an external .exe file with argument from unity, this what I have been doing:
ProcessStartInfo startInfo = new ProcessStartInfo("AAA.exe");
startInfo.WindowStyle = ProcessWindowStyle.Normal;
startInfo.Arguments = "MyArgument";
Process.Start(startInfo);
But an error keeps telling me that unity couldn't find the executable file.
How can I add a path or make unity find the executable file?
Advance Thanks.

It appears you may be giving an incorrect Path to your .exe
Try something like this:
string fullPath = Path.Combine(Environment.CurrentDirectory, "/YourSubDirectory/yourprogram.exe");
ProcessStartInfo startInfo = new ProcessStartInfo(fullPath);
startInfo.WindowStyle = ProcessWindowStyle.Normal;
startInfo.Arguments = "MyArgument";
Process.Start(startInfo);
Application Paths in Unity - Dependent on where your .exe is based, this may be of use.

Well, you may need to use a path relative to your unity build.exe's folder in order to make it work

You should either specify the full path or a relative path of the executable.
Check this post for some examples.

Related

Process.Start() results in Access Denied exception despite Full Control for Everyone group

My project output contains an executable called key_gen.exe that I'm trying to run. The executable is not built by the project, but copied in after the fact. With the code below I receive an Access Denied exception.
string key_gen_dir = Environment.CurrentDirectory.Substring(0, Environment.CurrentDirectory.LastIndexOf("\\")) + #"\Scripts\key_gen\";
Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = key_gen_dir + "key_gen.exe";
startInfo.Arguments = String.Format(". {0}", password);
process.StartInfo = startInfo;
process.Start();
The FileName value is correct and works when tested in Command Prompt. I do not want to run the program as an administrator. I have tried enabling UseShellExecute, but I receive a PlatformNotSupportedException. The key_gen_dir and all subfiles and folders have FullControl permissions for the Everyone group. If it's relevant, my project is targetted for .NET Standard 2.0.
I was able to resolve the access denied error by editing the file properties in Windows and adding the ALL APPLICATION PACKAGES group to the file permissions with Read & Execute allowed.

LPR command to print pcl-file from windows service not working(Now a tray application)

I've been looking around for a while for a possible solution and explanation, but I can't find anything really.
The following command is being run from a windows service. The same command does function if used directly in cmd.
It does not return any errors or anything else for that matter.
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 lpr.exe –S " + printerIP + " –P " + deviceName + " –o l " + fInfo.DirectoryName + #"\" + fInfo.Name;
process.StartInfo = startInfo;
process.Start();
It might just be some minor thing I'm missing, but I just can't see it. If it's possible to go around using the lpr-command with an easy alternative I'd love that, but I havent seen anything yet.
Edit:
Forgot to add that the file I'm trying to send to the printer is a pcl file.
Edit2:
When I run the command without the Hidden windowstyle and WaitForExit(5000) applied to the process then I can't seem to see any commandline written - all that appears is the empty command prompt.
Edit 3:
Been toying a bit around with this now and I've come up with the following:
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "lpr";
startInfo.Arguments = " –S " + printerIP + " –P " + deviceName + " –o l " + fInfo.DirectoryName + #"\" + fInfo.Name;
process.StartInfo = startInfo;
process.Start();
The above code works if it is executed by a user clicking a button in a form. So I decided to change my code into running as a tray application, seeing as I thought this might solve the issues - yet it still seems to refuse being run. Could it be some sort of issue with it being run by a triggered timer or another thread? Or perhaps something to do with the rights of those methods?
Change your code to this:
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 C:\windows\Sysnative\lpr.exe –S " + printerIP + " –P " + deviceName + " –o l " + fInfo.DirectoryName + #"\" + fInfo.Name;
process.StartInfo = startInfo;
process.Start();
The issue is that you are trying to access a 64 bit application (lpr) from a 32 bit cmd.exe application. The simple solution is to use the sysnative directory.
http://www.samlogic.net/articles/sysnative-folder-64-bit-windows.htm
The 'Sysnative' folder is invisible in Windows Explorer If you
start Windows Explorer and open the Windows folder on your hard disk,
you may notice that the Sysnative folder is not shown. The main reason
to this is that Windows Explorer is a 64-bit program (when run in a
64-bit Windows), and the Sysnative folder is only visible and
accessible from 32-bit software. If 64-bit software need access to the
64-bit system folder in Windows, the only option is to use the
System32 folder name (for example: C:\Windows\System32).
Using the 'Sysnative' folder will help you access 64-bit tools from
32-bit code Some tools in a 64-bit Windows only exist in a 64-bit
version; there is no 32-bit version available. And some of these tools
are located in the 64-bit System32 folder. One example is the nbtstat
tool that is used to help troubleshoot NetBIOS name resolution
problems. If you try to run the nbtstat tool from 32-bit code (for
example from an application or from script) and use a path like
C:\Windows\System32, you will get a "File not found" error. The file
can not be found; although Windows Explorer shows that the nbtstat
program file actually is located in the C:\Windows\System32 folder.
The solution to this (somewhat confusing) problem is to include the
virtual Sysnative folder in the folder path when you want to run the
tool. For example like this: C:\Windows\Sysnative\nbtstat.exe The
file path above will give you access to the 64-bit nbtstat tool from a
32-bit application or from a 32-bit script. We recommend you to read
this article / blog post (at Scottie’s Tech.Info) to get more details
about this.

Trying to run slui.exe from within a method using ProcessStartInfo and Process

I'm having an issue with running slui.exe from a method in c#. I'm using the code:
ProcessStartInfo startInfo = new ProcessStartInfo(#"C:\Windows\System32\slui.exe");
Process p = new Process();
p.StartInfo = startInfo;
p.Start();
p.WaitForExit();
but I keep getting a Win32Exception: 'The system cannot find the file specified'.
If I change the ProcessStartInfo to: (#"C:\Windows\System32\cmd.exe") it will launch just fine.
Is there something with running slui.exe in this context that is breaking?
I'm certain that the file is in the directory specified, so I'm stumped as to what may be going wrong here.
Any ideas how to call slui.exe from a c# method?
Slui.exe is only available as a 64-bit program on Windows x64. Your hard-coded path c:\windows\system32 will get re-directed to c:\windows\syswow64 when you run as a 32-bit process. And thus won't find the file.
Project + Properties, Compile tab, change the Platform target setting to "AnyCPU". Repeat for the Release configuration. And use Environment.GetFolderPath() to ensure it still works when Windows isn't installed to c:\windows.

Executing a batch file from Windows Forms application

I have written a small batch file that copies an exe from solution to the system32 folder.
copy "blah.exe" "%systemroot%/System32"
The batch file works fine and copies the exe if ran from the desktop by double clikcing (placed exe on the desktop as well)
However, I tried doing that from Windows Application by:
Process.Start("sample.bat");
(EXE file and batfile -> Properties -> Output to Copy Always)
The cmd window does come up, but the .exe file is not there in the destination directory. What am I missing here?
in your batch file change path to that particular folder where you have blah.exe, change to the particular drive and then to particular folderlets say your source folder is C:\test then type cd\test in the batch file, it should be something like:
C:
cd\test
copy "blah.exe" "%systemroot%/System32"
or use copy with complete path e.g
copy "C:\test\blah.exe" "%systemroot%/System32"
EDIT:
To copy using CMD try:
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 copy /b blah.exe %systemroot%/System32";
process.StartInfo = startInfo;
process.Start();
Edit 2: Or for batch file
System.Diagnostics.Process.Start("cmd", "/c sample.bat");

Running Latex from C#

When running latex from C# using Process.Start, I'm getting this error: "latex: A required file system path could not be retrieved." It runs fine from the command line, so I'm not sure why it doesn't run from Process.Start. Has anyone run into this issue?
Edit: Also, this is from ASP.NET!
Thanks!
Without seeing more code, my best guess would be to set the WorkingDirectory of your StartInfo class to whatever directory it works from on the command line.
ProcessStartInfo startInfo = new ProcessStartInfo(#"\path\to\latex\latex.exe");
startInfo.WorkingDirectory = #"\path\to\latex";
I've run into this problem before with other EXE's and that seemed to be the fix.
The issue was IIS permissions.

Categories