I'm trying to run the Windows System Assessment Tool (winsat.exe) using the following code:
System.Diagnostics.Process WinSPro =
new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo WinSSInfo =
new System.Diagnostics.ProcessStartInfo();
WinSSInfo.FileName = "cmd.exe";
WinSSInfo.Arguments = "/k winsat.exe";
WinSPro.StartInfo = WinSSInfo;
WinSPro.Start();
This code works if I only call cmd.exe, and even if I call regedit.exe it still works.
However, when I try to call winsat.exe as a argument of cmd.exe, it fails.
The command prompt shows this:
'winsat.exe' is not recognized as an internal or external command,
operable program or batch file.
I tried several ways to call winsat.exe:
Call it directly by assigning "winsat.exe" to ProcessStartInfo.FileName. It fails with a Win32Exception: The system cannot find the file specified
As above, using the full path - #"c:\windows\system32\winsat.exe". It fails with the same error.
Run the code as the System Administrator. It still fails.
Call winsat.exe as in the coded example. It failed as I explained earlier.
It's interesting that the command prompt launched from the code can only see .dll files in c:\windows\system32.
Does anyone have any idea why winsat.exe cannot be launched through System.Diagnostics.Process? Are there any limitations which I've misunderstood?
Thanks,
Rex
winsat.exe is redirected using Windows-on Windows 64-bit redirection. What's happening is that your launch request (from a 32-bit process) is being redirected to %windir%\SysWOW64\winsat.exe. Since there's no 32-bit version of this particular executable on 64-bit installs, the launch fails. To bypass this process and allow your 32-bit process to access the native (64-bit) path, you can reference %windir%\sysnative instead:
Process WinSPro = new Process();
ProcessStartInfo WinSSInfo = new ProcessStartInfo();
WinSSInfo.FileName = #"c:\windows\sysnative\winsat.exe";
WinSPro.StartInfo = WinSSInfo;
WinSPro.Start();
Alternatively, if you build your program as x64, you can leave the path as c:\windows\system32.
Note that it's best to use Environment.GetFolderPath to get the path to the windows directory, just in case the OS is installed in a non-standard location:
WinSSInfo.FileName = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.Windows),
#"sysnative\winsat.exe");
Based on Simon MᶜKenzie's answer, and the link he provided (thanks to soyuz for his comment) I wrote method that should work in either cases (to just copy/paste the code):
public static string GetSystem32DirectoryPath()
{
string winDir = Environment.GetFolderPath(Environment.SpecialFolder.Windows);
string system32Directory = Path.Combine(winDir, "system32");
if (Environment.Is64BitOperatingSystem && !Environment.Is64BitProcess)
{
// For 32-bit processes on 64-bit systems, %windir%\system32 folder
// can only be accessed by specifying %windir%\sysnative folder.
system32Directory = Path.Combine(winDir, "sysnative");
}
return system32Directory;
}
and code to launch the process:
var pi = new ProcessStartInfo
{
FileName = Path.Combine(GetSystem32DirectoryPath(), "winsat.exe"),
CreateNoWindow = true,
UseShellExecute = false
};
Process.Start(pi);
Related
I have been trying to launch Microsoft Teams through C#, by launching the shortcut.
However I am getting an error:
System.ComponentModel.Win32Exception: 'The specified executable is not a valid application for this OS platform.'
This is my code:
using System;
using System.Diagnostics;
namespace OnlineLearning
{
internal class Program
{
static void Main(string[] args)
{
ProcessStartInfo teams = new ProcessStartInfo(#"C:\Users\cesq\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Microsoft Teams.lnk");
teams.CreateNoWindow = true;
teams.RedirectStandardError = true;
teams.RedirectStandardOutput = true;
teams.RedirectStandardInput = true;
Process foo = Process.Start(teams);
}
}
}
I have been following an example from this question: Run an application via shortcut using Process.Start()
And did as one answer suggested:
Process proc = new Process();
proc.StartInfo.FileName = #"C:\Users\cesq\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Microsoft Teams.lnk";
proc.Start();
Sadly this didn't work (gave the same error).
Also the accepted answer:
Setting UseShellExecute = false was the problem. Once I removed that, it stopped crashing.
Didn't work for me, since I already have done what it suggested.
Also this question: How to run a shortcut
did not help me as well, as it is not relevant to my example.
As well as this one: 'The specified executable is not a valid application for this OS platform
Now I am out of ideas, so I am asking for your help.
I need to let a .reg file and a .msi file execute automatically using whatever executables these two file types associated with on user's Windows.
.NET Core 2.0 Process.Start(string fileName) docs says:
"the file name does not need to represent an executable file. It can be of any file type for which the extension has been associated with an application installed on the system."
However
using(var proc = Process.Start(#"C:\Users\user2\Desktop\XXXX.reg")) { } //.msi also
gives me
System.ComponentModel.Win32Exception (0x80004005): The specified executable is not a valid application for this OS platform.
at System.Diagnostics.Process.StartWithCreateProcess(ProcessStartInfo startInfo)
at System.Diagnostics.Process.Start()
at System.Diagnostics.Process.Start(ProcessStartInfo startInfo)
at System.Diagnostics.Process.Start(String fileName)
with ErrorCode and HResult -2147467259, and NativeErrorCode 193.
The same code did work in .Net Framework 3.5 or 4 console app.
I can't specify exact exe file paths as the method's parameter since users' environments are variant (including Windows versions) and out of my control. That's also why I need to port the program to .Net Core, trying to make it work as SCD console app so that installation of specific .Net Framework or .NET Core version is not required.
The exception is thrown both in Visual Studio debugging run and when published as win-x86 SCD. My PC is Win7 64bit and I'm sure .reg and .msi are associated with regular programs as usual Windows PC does.
Is there solution for this? Any help is appreciated.
You can also set the UseShellExecute property of ProcessStartInfo to true
var p = new Process();
p.StartInfo = new ProcessStartInfo(#"C:\Users\user2\Desktop\XXXX.reg")
{
UseShellExecute = true
};
p.Start();
Seems to be a change in .net Core, as documented here.
See also breaking changes.
You can set UseShellExecute to true and include this and your path in a ProcessStartInfo object:
Process.Start(new ProcessStartInfo(#"C:\Users\user2\Desktop\XXXX.reg") { UseShellExecute = true });
In case this bothers you as well:
For those of us that are used to simply calling Process.Start(fileName); the above syntax may give us anxiety... So may I add that you can write it in a single line of code?
new Process { StartInfo = new ProcessStartInfo(fileName) { UseShellExecute = true } }.Start();
You have to execute cmd.exe
var proc = Process.Start(#"cmd.exe ",#"/c C:\Users\user2\Desktop\XXXX.reg")
don't forget the /c
use this to open a file
new ProcessStartInfo(#"C:\Temp\1.txt").StartProcess();
need this extension method
public static class UT
{
public static Process StartProcess(this ProcessStartInfo psi, bool useShellExecute = true)
{
psi.UseShellExecute = useShellExecute;
return Process.Start(psi);
}
}
string itemseleccionado = lbdatos.SelectedItem.ToString();
var p = new Process();
p.StartInfo = new ProcessStartInfo(itemseleccionado)
{
UseShellExecute = true
};
p.Start();
I am trying to run ".sh" file from c# core application.But it doesn't seem to be running properly.Here is my scenario.
I am working on .Net core project which is hosted on Linux environment.We are trying to create "PDF" in our project for which we have used "Apache FOP".
Here i have created one "shell script" file "transform.sh" which internally calls "fop" with required parameters.Since developement is being done on windows machine we tested the same usinf "batch" file i.e. "transform.bat",but since we cannot use the "batch" file on linux enviornment we have created shell script file "transform.sh"
Following is the code from"transform.sh"
./fop -xml $1 -xsl $2 -pdf $3
Following is C# code from which i am calling the "shell script file
var process = new Process
{
StartInfo = new ProcessStartInfo
{
UseShellExecute = false,
RedirectStandardOutput = true,
Arguments = string.Format("{0} {1} {2}", XML_filename, XSL_filename, output)
}
};
process.StartInfo.FileName = "Path to shell script file";
process.Start();
process.WaitForExit();
Above code doesnot give any error but it also does not create the pdf file.If i directly run the shell script file from "Terminal" it works fine and create pdf file.
./transform.sh "/home/ubuntu/psa//PdfGeneration/ApacheFolder/XMLFolder/test.xml" "/home/ubuntu/psa/PdfGeneration/ApacheFolder/XSLTFolder/Certificate.xsl" "/home/ubuntu/psa/PdfGeneration/ApacheFolder/PDFFolder/t444t.pdf"
Please let me know if there is something wrong i am doing?How can i make the sheel script run on linux through C# core application.
Thanks.
I was able to solve the issue,just thought that i should put my solution here so that it may help others in future...
As mentioned in Question i was not able to generate the PDF file through shell script on linux machine.After debugging as suggested by "#JNevill" I came to understand that the shell script file was not getting called from .net process itself.
So my first task was to make the shell script file called through .Net Process.
After lots of searching through Net and trying out different solutions i got solution at How to perform command in terminal using C#(Mono).
So changed my code of calling the process as follow,
var command = "sh";
var myBatchFile = //Path to shell script file
var argss = $"{myBatchFile} {xmlPath} {xsltPath} {pdfPath}"; //this would become "/home/ubuntu/psa/PdfGeneration/ApacheFolder/ApacheFOP/transform.sh /home/ubuntu/psa/PdfGeneration/ApacheFolder/XMLFolder/test.xml /home/ubuntu/psa/PdfGeneration/ApacheFolder/XSLTFolder/Certificate.xsl /home/ubuntu/psa/PdfGeneration/ApacheFolder/PDFFolder/test.pdf"
var processInfo = new ProcessStartInfo();
processInfo.UseShellExecute = false;
processInfo.FileName = command; // 'sh' for bash
processInfo.Arguments = argss; // The Script name
process = Process.Start(processInfo); // Start that process.
var outPut = process.StandardOutput.ReadToEnd();
process.WaitForExit();
After changing the code ,the ".sh" file got executed and i was able to generate the PDF file.
Also script of the ".sh" file i.e. (transform.sh) which was calling Apache FOP file i.e. "FOP.sh" also needed to be changed.
Initially code was
./fop -xml $1 -xsl $2 -pdf $3
Which i changed as follow,(Change was to give full path of the FOP file)
/home/ubuntu/psa/PdfGeneration/ApacheFolder/ApacheFOP/fop -xml $1 -xsl $2 -pdf $3
Late answer, but for me, it worked just by setting the RedirectStandardOutput to true and changing the FileName property like this:
processInfo.FileName = #"C:\Program Files\Git\git-bash.exe";
processInfo.RedirectStandardOutput = true;
I have written a simple custom shell application for Windows 8.1 systems in WPF. It functions fine, but I need to start some of the applications in the Run section of the registry. Fine and good, however, no matter what I try, they don't launch, and I receive an error: "The system cannot find the file specified."
This is designed for 64 bit systems, so I've heard that using C:\Windows\Sysnative\ for the path rather than C:\Windows\System32\ is a fix, but it didn't work. My code is as follows:
Process processToStart = new Process
{
StartInfo =
{
FileName = #"C:\Windows\Sysnative\hkcmd.exe",
WorkingDirectory = #"C:\Windows\Sysnative\")
}
};
processToStart.Start();
The way I found to get this to work was to disable WOW64 file system redirection. Nothing else seemed to work.
From this link:
http://tcamilli.blogspot.co.uk/2005/07/disabling-wow64-file-system.html
[DllImport("Kernel32.Dll", EntryPoint="Wow64EnableWow64FsRedirection")]
public static extern bool EnableWow64FSRedirection(bool enable);
Wow64Interop.EnableWow64FSRedirection(false)
Process processToStart = new Process
{
StartInfo =
{
FileName = #"C:\Windows\Sysnative\hkcmd.exe",
WorkingDirectory = #"C:\Windows\Sysnative\")
}
};
processToStart.Start();
Wow64Interop.EnableWow64FSRedirection(true)
Not sure if these might be the cause of your issue but from your example posted in the question, note these few points:
StartInfo.FileName should only contain the fileName, not the path.
If you're trying to execute hkcmd.exe, you wrote it as hkcmnd.exe (extra N).
In the example above I believe it effectively looks like specifying the filename and workingdirectory repeatedly causes a file to not be found. See this link I used to check and this link as well
Sysnative was not present on my machine (Win 7 x64), it might be in Windows 8.
I couldn't get hkcmd.exe to execute, it threw an error you had as well, however, executing cmd.exe and notepad.exe is fine.
Sample code:
System.Diagnostics.Process processToStart = new System.Diagnostics.Process();
processToStart.StartInfo.FileName = "cmd.exe"; //or notepad.exe
processToStart.StartInfo.WorkingDirectory = #"C:\Windows\System32\";
processToStart.Start();
I am trying to write some c# code to start a browser using Process.Start(app,args); where apps is the path to the browser e.g. /Applications/Google Chrome.app/Contents/MacOS/Google Chrome and the args are --no-default-browser-check
If i do, which works on Windows and on Linux
Process.Start("/Applications/Google Chrome.app/Contents/MacOS/Google Chrome","--no-first-run");
I get
open: unrecognized option `--no-first-run'
Usage: open [-e] [-t] [-f] [-W] [-n] [-g] [-h] [-b <bundle identifier>] [-a <application>] [filenames]
Help: Open opens files from a shell.
By default, opens each file using the default application for that file.
If the file is in the form of a URL, the file will be opened as a URL.
Options:
-a Opens with the specified application.
-b Opens with the specified application bundle identifier.
-e Opens with TextEdit.
-t Opens with default text editor.
-f Reads input from standard input and opens with TextEdit.
-W, --wait-apps Blocks until the used applications are closed (even if they were already running).
-n, --new Open a new instance of the application even if one is already running.
-g, --background Does not bring the application to the foreground.
-h, --header Searches header file locations for headers matching the given filenames, and opens them.
I have also tried Monobjc to try run the code with
// spin up the objective-c runtime
ObjectiveCRuntime.LoadFramework("Cocoa");
ObjectiveCRuntime.Initialize();
NSAutoreleasePool pool = new NSAutoreleasePool();
// Create our process
NSTask task = new NSTask();
NSPipe standardOut = new NSPipe();
task.StandardOutput = standardOut;
task.LaunchPath = #"/Applications/Google Chrome.app/Contents/MacOS/Google Chrome";
// add some arguments
NSString argumentString = new NSString("--no-first-run");
NSArray arguments = NSArray.ArrayWithObject(argumentString);
task.Arguments = arguments;
// We should have liftoff
task.Launch();
// Parse the output and display it to the console
NSData output = standardOut.FileHandleForReading.ReadDataToEndOfFile;
NSString outString = new NSString(output,NSStringEncoding.NSUTF8StringEncoding);
Console.WriteLine(outString);
// Dipose our objects, gotta love reference counting
pool.Release();
But when I run my code using NUnit it causes NUnit to blow up.
I suspect that this is a bug but can't prove it. I appreciate any and all help!
To make Process.Start use exec directly instead of using the OS' mechanism for opening files, you must set UseShellExecute to false. This is also true on Linux and Windows.
Process.Start(new ProcessStartInfo (
"/Applications/Google Chrome.app/Contents/MacOS/Google Chrome",
"--no-first-run")
{ UseShellExecute = false });
Note that you can also use 'open' for your use case, to run the Chrome app bundle properly. Use the '-a' argument to force it to run a specific app, the '-n' argument to open a new instance, and '--args' to pass in arguments:
Process.Start(new ProcessStartInfo (
"open",
"-a '/Applications/Google Chrome.app' -n --args --no-first-run")
{ UseShellExecute = false });
Looks like Process uses the open command line utility to launch.
You should avoid calling the executable directly. If the application is already running, this would launch a second instance of it instead of activating the already running instance. That's probably not what you want, and not all applications can handle this anyway.
With open, the syntax to launch Chrome would be
open -a Chrome
I don't know how the Process class works on MacOS X, but I assume that the parameters should be similar.
Note, if you just want to open a web page, you should not specify the executable; instead, just pass the URL, so that it will be opened in the user's default browser. This is valid for any platform.
Process.Start("http://www.google.com");
Have you tried something like concatenating the parameters into the process name instead of passing it separated?
var processName = "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome";
var args = "--no-default-browser-check";
Process.Start(String.Format("{0} {1}", processName, args));
Why dont you try something like this:
Process P = new Process();
P.StartInfo.FileName = "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome";
P.StartInfo.Arguments = "--no-default-browser-check";
P.UseShellExecute = false;
P.Start();