Is there a way to run an application via shortcut from a C# application?
I am attempting to run a .lnk from my C# application. The shortcut contains a significant number of arguments that I would prefer the application not have to remember.
Attempting to run a shortcut via Process.Start() causes an exception.
Win32Exception: The specified executable is not a valid Win32 application
This is the code I am using.
ProcessStartInfo info = new ProcessStartInfo ( "example.lnk" );
info.CreateNoWindow = true;
info.UseShellExecute = false;
info.RedirectStandardError = true;
info.RedirectStandardOutput = true;
info.RedirectStandardInput = true;
Process whatever = Process.Start( info );
Could you post some code. Something like this should work:
Process proc = new Process();
proc.StartInfo.FileName = #"c:\myShortcut.lnk";
proc.Start();
Setting UseShellExecute = false was the problem. Once I removed that, it stopped crashing.
if your file is EXE or another file type like ".exe" or ".mkv" or ".pdf" and you want run that with shortcut link your code must like this.
i want run "Translator.exe" program.
Process.Start(#"C:\Users\alireza\Desktop\Translator.exe.lnk");
If you're using UseShellExecute = false and trying to launch a batch file make sure to add .bat to the end of the filename. You don't need .bat if UseShellExecute = true though. This made me just waste an hour of work... hoping to save someone else.
Related
I am working on an NSIS installer (windows forms application).
The main idea of the application - user goes through a windows forms application, configures all the needed settings and when he/she clicks on "finish" an .nsis file gets generated automatically. makeNSIS.exe is also included in my application, so that a user does not have to install it in order to use my installer.
My goal is to silently produce a setup.exe file. I want to pass my automatically generated .nsi file as an argument to the makeNSIS.exe per cmd, tell it to execute it and create a setup.exe file at a certain path. And I want teh whole thing done silently.
One of my failed trials:
ProcessStartInfo psi = new ProcessStartInfo();
//my automatically generated nsis file
psi.Arguments = Application.StartupPath + "\\NSIS\\NSIS SG Project\\NSIS\\My application.nsi";
psi.CreateNoWindow = true;
psi.WindowStyle = ProcessWindowStyle.Hidden;
//nsis application that is included in my project so that users wouldn't have to install it
psi.FileName = Application.StartupPath + "\\NSIS\\makeNSIS.exe";
Process.Start(psi);
What am I doing wrong?
NSIS.exe is not the compiler, MakeNSIS.exe is! The Stubs, Plugins and Include directories are also required and probably the Contribs directory as well.
I know this is old question, but i have one of possible solution for this.
If you write app in C# (WinForm, WPF etc.) you can use this:
ProcessStartInfo startInfo = new ProcessStartInfo
{
CreateNoWindow = true,
UseShellExecute = false,
RedirectStandardOutput = true,
//Path to makensis.exe
FileName = "C:\\NSIS\\makensis.exe",
//make the window Hidden
WindowStyle = ProcessWindowStyle.Hidden,
//Source to your nsis script
Arguments = "D:\\Script\\myapp.nsi"
};
try
{
using (Process exeProcess = Process.Start(startInfo))
{
exeProcess.WaitForExit();
}
}
catch (Exception exc)
{
// handle exception
}
In many cases this is a universal solution for me.
I have a .bat file to be executed.
Inside that .bat file, at its end is that code
START _file_creator.bat %some_arg_name%
ENDLOCAL
EXIT
I don't want to show the window during the execution and also I must wait until the operation doing by this .bat file is completed and then terminate the execution (at the end of the operation I see the standard text "Press any key to continue"). I need also to check the output and errors by that file, so I'm trying to use that code:
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo.FileName = #"C:\m_f\_config.bat";
proc.StartInfo.RedirectStandardError = true;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.CreateNoWindow = true;
proc.Start();
proc.WaitForExit();
output1 = proc.StandardError.ReadToEnd();
proc.WaitForExit();
output2 = proc.StandardOutput.ReadToEnd();
proc.WaitForExit();
But all I get is the error
Windows can not find file "_file_creator.bat".
Make sure you typed the name correctly and try again.
of course if I run that .bat file with the proc.StartInfo.UseShellExecute = true it works fine, but in that case I can't set RedirectStandardError = true and RedirectStandardOutput = true
How to fix it ?
Edit
using that code it works now
proc.StartInfo.FileName = #"C:\m_f\_config.bat";
proc.StartInfo.WorkingDirectory = #"C:\m_f\";
Try setting the working directory correctly or make sure that _file_creator.bat is somewhere in the PATH. See the documentation about the working directory in conjunction with UseShellExecute:
The WorkingDirectory property behaves differently depending on the value of the UseShellExecute property. When UseShellExecute is true, the WorkingDirectory property specifies the location of the executable. If WorkingDirectory is an empty string, it is assumed that the current directory contains the executable.
When UseShellExecute is false, the WorkingDirectory property is not used to find the executable. Instead, it is used only by the process that is started and has meaning only within the context of the new process. When UseShellExecute is false, the FileName property must be a fully qualified path to the executable.
Hey all,
I'm trying to run an exe file ON A REMOTE MACHINE (not from, but ON).
I have very simple code as following:
ProcessStartInfo info = new ProcessStartInfo("C:\\PsTools");
info.FileName = "psexec \\\\" + machine.Name + "\\C\\Program Files\\test.exe";
info.RedirectStandardOutput = true;
info.UseShellExecute = false;
Process p = Process.Start(info);
When trying to run this code i get "The system cannot find the file specified" error.
There is a file named "test.bat" on the specified directory.
The remote machine is on the same domain and the C folder is shared (I'm the admin).
I have PsTools installed and configured as environment variables.
I have tried variety of codes (for example if i don't use "psexec" on the ProcessStartInfo constructor and on the FileName property, the bat file runs on the local machine instead of the remote one...) and nothing works!
any ideas?
My guess is that it's failing to find psexec, because you've set UseShellExecute to false. Try giving the full path to psexec.exe.
You should also set the FileName property to just the file you want to start, and the Arguments property to the command line arguments, like this:
ProcessStartInfo info = new ProcessStartInfo("C:\\PsTools");
info.FileName = #"c:\whatever\psexec.exe";
info.Arguments = #"""\\" + machine.Name + #"\C\Program Files\test.exe""";
info.RedirectStandardOutput = true;
info.UseShellExecute = false;
Process p = Process.Start(info);
Note that I've also added double quotes in the Arguments property so that it doesn't get split into two arguments due to "Program Files" having a space in it.
the following code is to open a console application (which uses pdcurses for output, nothing special):
myProcess.StartInfo.FileName = "some.exe";
myProcess.StartInfo.UseShellExecute = false;
myProcess.StartInfo.CreateNoWindow = false;
myProcess.StartInfo.RedirectStandardInput = true;
myProcess.Start();
The Problem is that it opens the designated window but directly closes it (it's barely visible). Starting the program without RedirectStandardInput works. The problem is that it does not throw an exception nor any error-message. What is wrong with my code? How can I write input to the program? Thanks.
Is some.exe a console program?
You could try starting cmd.exe with the /K switch and pass your some.exe as the argument to it.
I would like to mimic the Run command in Windows in my program. In other words, I would like to give the user the ability to "run" an arbitrary piece of text exactly as would happen if they typed it into the run box.
While System.Diagnostics.Process.Start() gets me close, I can't seem to get certain things like environment variables such as %AppData% working. I just keep getting the message "Windows cannot find '%AppData%'..."
You can use the Environment.ExpandEnvironmentVariables method to turn %AppData% into whatever it actually corresponds to.
Depending on what you're trying to do, you could also call CMD.EXE, which will expand your environment variables automatically. The example below will do a DIR of your %appdata% folder, and redirect the stdOut to the debug:
StreamReader stdOut;
Process proc1 = new Process();
ProcessStartInfo psi = new ProcessStartInfo("CMD.EXE", "/C dir %appdata%");
psi.RedirectStandardOutput = true;
psi.UseShellExecute = false;
proc1.StartInfo = psi;
proc1.Start();
stdOut = proc1.StandardOutput;
System.Diagnostics.Debug.Write(stdOut.ReadToEnd());