Calling .bat file from c# - c#

Using this piece of code .exe running in server
string bat =null;
bat = "D:/folder/a.bat";
System.Diagnostics.Process.Start(bat);
Error: Could not find the specified file.
Can anyone help me on this.

Make sure the file really is located at that path.
Make sure your program has access to this path.
Use backslashes: bat = #"D:\folder\a.bat";

Filepath in Windows doesn't take a forward slash, it's not a URL/URI.
Use backslashes.
Anyone of below should work if the program has access to the bat file.
string bat=#"D:\folder\a.bat";
or
string bat="D:\\folder\\a.bat";
Also, checking for the existence of the bat file will be a good practice here:
if(File.Exists(bat))
{
System.Diagnostics.Process.Start(bat);
}

change the slashes to backslashes:
bat = "D:\\folder\\a.bat";

Typically you need to run an executable (like cmd.exe) and then pass it a parameter. cmd.exe specifically has two options /C (carries out the command specified by the string, then terminates) and /K (carries out the command specified by the string but remains open)

Wrong path. Try
bat = #"D:\folder\a.bat";

Related

Process.Start working directory in the same string as filepath

Is there any way to specify working directory as below?
Process.Start("c:\someDir\someExecutable.exe + working directory path");
or how to make windows environment variable path work with :
Process.Start("c:\someDir\someExecutable.exe");
I know that ProcessStartInfo can be use to specify working directory.
I have my own reason to for wanting to put working directory and file path in same parameter of Process.Start().
The documentation for public static Process Start(string fileName) is found here: https://msdn.microsoft.com/en-us/library/53ezey2s(v=vs.110).aspx
As you can see the description for that parameter is:
The name of a document or application file to run in the process.
The remarks also note:
This overload does not allow command-line arguments for the process. If you need to specify one or more command-line arguments for the process, use the Process.Start(ProcessStartInfo) or Process.Start(String, String) overloads.
So in summary no, you can't do this. Even if your program accepted a working directory as a command line argument this overload will not work.

How to execute a command from C#?

I am trying to execute a code from c# windows form application.
string cmdCode = "/C mogrify -crop 590x389+116+102! D:\\Backup\\Images\\*.TIF";
System.Diagnostics.Process.Start("CMD.exe", cmdCode);
But It doesnt do what it is supposed to do?
the code perfectly does the job when I type it in the command line. I already tried to change the path. I placed the tif files with MyApp.exe file and changed the cmdCode as
cmdCode = "/C mogrify -crop 590x389+116+102! *.TIF";
no success.. It shows up the black command promt very quickly and it gets disappeared.
I also tried to put the code in a file and make the extension .bat to run it but still no success!! any suggestions ?
Thanks
Try replacing the /C option with /K.
That will not solve your problem, but it should prevent the command prompt from disappearing, and allow you to see if any error is displayed by the prompt.
It's also possible that the PATH you are passing to CMD.exe is different from the one used by default, for some reason. Once you've started cmd.exe with the /K option, you should also be able to issue a echo %PATH% command, and see what you've got.
Remove the cmd.exe part and instead place there the path to mogrify.exe and start the parameters with "-crop...", removing the /C
If you're passing along path names that contain spaces you'll need to enclose the path with quotes, so the parameters will look like this:
"-crop \"590x389+116+102!\" \""+YourPathHere+"\"";

Include a batch file in a batch file

I have a problem calling a batch file from another batch file when trying to run everything by using Process.Start. Basically I call the execution of a batch file from my c# program that looks like this:
call include.bat
//execute the rest of the batch file here
The include.bat file sets up paths and can be used by a number of other batch files. When I run the Process.Start sometimes this works and sometimes I get ERROR: cannot find include.bat. First of all any idea why this happens? And ideas on how to fix this from the batch file?
To switch to the directory your batch file is located in, use this:
cd %~dp0
I do this in almost all of my batch scripts. That way relative paths should always work.
I know this is an old question but I thought it would be worth noting that the approach promoted by the accepted answer (i.e. changing the working directory) may not always be appropriate.
A better general approach is to refer to dependencies by full path:
call "%~dp0include.bat"
(Since %~dp0 already ends with a backslash, we don't need to add another one.)
Here are some benefits of not changing the working directory:
The rest of the batch file can still use the original working directory.
The original working directory in the command prompt is preserved, even without "SETLOCAL".
If the first batch file is run via a UNC path (such as "\\server\share\file.bat"), the full-path call will succeed while changing the directory (even with "cd /d") will fail. (Using pushd/popd would handle this point, but they have their own set of problems.)
These benefits are particularly important for alias-type batch files, even if they are not as important for the specific situation that motivated this question.
Before the script, try CD /D %~dp0
First thing I'd try is to use full path information in the call statement for include.bat. If that fixes it, you probably are just not running the batch file from the proper location. I'm sure there's a "working directory" capability in C#, I'm just not sure what it is.
Do you set ProcessStartInfo.WorkingDirectory ( http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.workingdirectory.aspx ) on the ProcessStartInfo that you pass to Process.Start?
Since include.bat sometimes cannot be found, working directory may be wrong (not the folder where include.bat is located).

Launching a .pl file using Process.Start

I have been using Process.Start to launch executables (.exe) files. Now I need to execute a .pl file with some arguments. can I still use Process.Start or I need a different approach?
EDIT :- I am having to mark this question unanswered as I am getting the following error when I try to call the perl file from the CSharp code:- (When I call the same from the commandline with the same path and parameters, It works fine)
System.ApplicationException: StartProcess Failed
System.ComponentModel.Win32Exception: The specified executable is not a valid application for this OS platform)
Please note that when I try to call an .exe file from my C# code, I dont see the above error.
EDIT:-
Checking the following link now:- How do I call Perl script in C# application?
It seems that the ProcessStartInfo constructor has two parameters - fileName and the arguments. You should set Perl.exe as the fileName and the "argument" would be your perl file (.pl) with other arguments It accepts. Checking now....
You certainly can :) you can also pass it arguments by adding them after the file name in
Process.Start(file.pl args1 args 2);
It will load the file with your default application for .pl files, the other option is to specify the software then pass your file as a parameter providing you have the right software to handle the file it should be fine
Process.Start() can be pointed at any file and it will be opened using the default software or that which you specify, it need not be an executable.
Yes you can, Process.Start() takes a string parameter, what you pass for this parameter does exactly the same thing it would do if you entered the same string in the windows start -> run dialog.

Implement "Open Containing Folder" and highlight file

This can be a handy functionality to have in a program that works with files/folders. It's easy enough to actually open the containing folder using:
System.Diagnostics.Process.Start( *path to folder* );
...but how do I go about actually selecting the target file within that parent folder? If I use the Process.Start method it actually attempts to open the file.
According to Windows Explorer Command-Line Options you just need to start an explorer process with /select parameter.
For instance, 'explorer /select,c:\Windows' will open a window with c:\windows folder selected.
So simply Process.Start("explorer.exe", "/select," + filename) should be enough.
Execute Explorer.exe with /select, "filename" command line argument
System.Diagnostics.Process.Start(
"explorer.exe",
string.Format("/select, \"{0}\"", filename));
Containing folder, Self directory is represented in many ways!!!
Simple 2 ways are . and, .\. no idea what is the difference!.. :D
From DOS and bat files... Start . or Start .\. (Y)
Try... these 2 works, but check whether this is the solution u expect!
System.Diagnostics.Process.Start("explorer.exe", #".\.");
Or
System.Diagnostics.Process.Start("explorer.exe", #".");
-
Sometimes the application is run from a temp directory or a different dir (eg: in Sandbox... or while being scanned by antivirus... etc. :)

Categories