so i need a simple function to run bat file.
How to do such thing?
Process.Start("file.bat");
Take a look at Process.Start
In the simplest form, you can call it like this
Process.Start("thebatchfile.bat")
You may find you need to include the directory in the call;
using System.Diagnostics;
Process.Start(#"C:....\thebatfile.bat");
If for example the file takes arguments in (not for Bat file granted), they too can be added in the "Start()"method. Seperate Start("....thebatfile", "-s - t 3600"); with a comma. Intellisense is great for looking at the overload options of the method.
I don't know how this is any different, but in a project I inherited, we used
Microsoft.VisualBasic.Interaction.Shell.
Shell("file.bat", AppWinStyle.NormalFocus, True, -1)
The last two parameters specify if the calling program should wait for the command to finish, and how long it should wait for it to time out (-1 for forever)
Related
I call a console program in a c# application but it's not working.
I'm trying to call pdf toolkit VIA c#.
When I run pdftk using command prompt, the output file (new.pdf) is created. When c# calls pdftk it doesn't error, but it doesn't work.
Am I calling it wrong in C# or something?
C# Call: (Not working)
System.Diagnostics.Process.Start(pdfTKPath, pdftkArgs);
Console.Write(pdfTKPath + pdftkArgs);
Console.ReadLine();
// Here's the string that the console outputs.
// pdftk.exe C:\Users\name\Desktop\assignment1\assignment1\data\temp\test_cropped.pdf stamp C:\Users\name\Desktop\assignment1\assignment1\data\temp\test_footer.pdf output C:\Users\name\Desktop\assignment1\assignment1\data\temp\new.pdf
CMD Prompt Call: (Works Fine)
C:\Users\name>pdftk.exe c:\Users\name\Desktop\assignment1\assignment1\data\temp\test_cropped.pdf stamp c:\Users\name\Desktop\assignment1\assignment1\data\temp\test_footer.pdf output c:\Users\name\Desktop\assignment1\assignment1\data\temp\new.pdf
C:\Users\name>pdftk.exe
is wrong
Use the following one:
C:\Users\name\pdftk.exe
Also, did you escape
\
symbol? It is required if you are hardcoding path in the app
Maybe pdftk.exe is not in the path. Try to call the exe specifying it's full path.
firstly I would check if aprocess is created and if it has an error
var processo=System.Diagnostics.Process.Start(pdfTKPath, pdftkArgs);
Console.Write("process created: "+processo.ExitCode);
The reason that this wasn't working is that the processing that was happening with the prior method wasn't complete.
The method before this one uses ghost script to create a cropped PDF file. This method was evoked without ghost script completing the work. Therefore this method didn't think the files existed yet (because they didn't). I set this method to wait 2 second before starting and it works fine. This allowed the previous method time to complete.
thanks for taking the time to read my quandary.
I need to open a PDF (C# 4.0, winforms). Now normally this is pretty easy if you aren't passing in arguments (Process.Start to the PDF), but I need to be able to pass in arguments (go to a specific page or named destination right now, maybe more later).
Now I know if you call AcroRd32 you can pass in the arguments needed to do this, but it is possible that the user won't have Acrobat Reader installed on their machine, so I don't really want to call that exe necessarily. Anyone know of a way to pull this off?
Am I missing an obvious way to do this using something else?
Thanks for your help!
PS - replies can be in C# or VB, don't care
To pass the arguments in Process.Start(), you need to call AcroRd32.exe or Acrobat.exe, like:
Process myProcess = new Process();
myProcess.StartInfo.FileName = "acroRd32.exe"; //not the full application path
myProcess.StartInfo.Arguments = "/A \"page=2=OpenActions\" /n C:\\sample.pdf";
myProcess.Start();
I suggest putting this is a Try/Catch block, and if it fails then try "Acrobat.exe" as the FileName in another, nested Try/Catch block, and if that fails, default to:
Process.Start("C:\\sample.pdf");
which doesn't allow you to open to the specific page, but at least the PDF will open if the first two approaches have failed.
So I am trying to launch a printer script using cscript from C#, and cscript launches a visual basic file. So sort of a daisy chain (and I want to keep this daisy chain intact for certain reasons).
Here's the code:
Process.Start("c:/windows/system32/cscript.exe c:/windows/System32/Printing_Admin_Scripts/en-US/prnport.vbs");
Now, when I launch ONLY cscript, no problems.
However when I add the condition of prnport.vbs to the cscript launch, I get this error in Visual Studio:
"The system cannot find the file specified"
But I can confirm the file path is correct - prnport.vbs DOES exist in /en-US.
So what am I doing wrong here? Can you not pass arguments (and in this case, the file path is being passed as an argument to cscript.exe) when using Process.Start?
New to C# and confused about the proper way to do this.
You have to specify the arguments separately from the file to run. Try the Process.Start(string, string) overload:
Process.Start("c:/windows/system32/cscript.exe",
"c:/windows/System32/Printing_Admin_Scripts/en-US/prnport.vbs");
That's an Argument, you'll need to use another overload of Process.Start
Have a look at the method's documentation.
Process.Start (String, String) will do, others are possible and offer more flexibility, if you should need that, too.
The Process.Start expects the file name as the first parameter. The arguments are given in separate argument.
The C# Process class allows you to run a command-line executable in Windows. The Process.StartInfo class holds information about the command line job you want to run, including its path and filename.
My problem is that Process.Start() will throw an exception if the file does not exist. To try and avoid the exception, I coded a check to see if the file existed, but this did not work properly in all instances because a name might not exist as a file with that exact name, but might have an executable counterpart. For example, "C:\SoAndSo" might not exist as a file, but "C:\SoAndSo*.exe*" does, and would have been found successfully when starting the Process command.
I have hacked my checks to try the name given to me, and the name + ".exe" and ".bat", but this feels cludgy, and perhaps I am missing other executable extensions.
So: is there a command to ask the question: 'Do you have an executable version of the filename starting 'SoAndSo'?
Why not try to start the Process and wrap that in a try/catch and when you get a FileNotFoundException then you can simply print the output of that and exit? I see you want to try and avoid the exception but you might have more overhead trying to prevent it than just incurring it.
id had a similar requirment.. & i solved it using the environment variable %PATHEXT%.
reference can be found here,
say I'm trying to find a program called abcd then the app would look for
*abc*. with all extension combo available ion the %PATHEXT% variable..
I'd say this is a crude implementation.. but it worked for me.. I;m quite sure there is a better way to do this.
I want to develop a command line tool. I didn't build one before, but I used a few, (like mercurial). What steps do I need to take to know how to do that? So what is the problem:
Regular console application need to be invoked from the command line only from it's directory. Like: C:\Projects\CommanLineProject\MyProjectConsole.exe. I want to use it from the command line from any directory, like mercurial.
I don't sure how to parse and how to use the command-line arguments. There are programs that taking this arguments: c:\>MyProject "c:\point to this path" /xml:"here is the another path" /r.
A command line tool is just a regular console application. You can use the string[] args parameter from the Main method to retrieve any arguments that are passed to the application.
I would go look at this stack overflow question as it has links to some good command line parsing libraries in the answers.
One thing you'll need to do, if the application is to take any but the most basic arguments, is select a cmd line parsing library, as there is not one provided by the Framework.
I would look into the following link before making a significant command line application.
http://commandline.codeplex.com/
For question/edit #1:
If you add the path to the executable to your operating system's 'Path' environment variable, you can run the application from any directory. More info on setting it for Windows XP.
Also, you may need to retrieve the current working directory when the application is started. I believe you can use Environment.CurrentDirectory to get this. I'd suggest saving it in case you need to do something that might change the current directory for your process.
For question/edit #2:
You can use something like this to parse the actual arguments that are passed into the application via the args string array. However, what you do with the arguments after that point is completely up to you. There's nothing stopping you from just going through the array and dealing with the arguments manually (foreach (string arg in args) {...}) if you prefer.
If you want to make the first command line argument the path to a file to write to, so be it. If you want to have an optional argument like /verbose then that works as well. You are really only limited to your imagination (and the limits of what can be legally typed on the command line :) )