terminal command not working in c# progra - c#

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.

Related

Sending generated code elsewhere to NetLogo

I'm trying to work on an environment that its main function is to adopt Visual Programming to create NetLogo code (similar to Google's Blockly).
Right now, I'm using Unity3D to do the job and wondering if it's possible to access NetLogo from it. The objective is to send the generated code directly into the Code Tab, opening a blank project already with the code in the tab (without the user copying and pasting it there).
What I know up until now is that I can open NetLogo from Unity with a function called Process.Start, which takes 2 arguments: the first is the name of the target program to be executed ("NetLogo.exe"), the second one is a list of arguments that can be passed to the targeted program, which solely depends on each program, as found here and here. However, I didn't understand much about these arguments, which is why I recurred to ask.
Do I need to also work on a Java/Scala environment to do this for me with the Extensions API, or can I use these arguments in Process.Start to do it?
Thanks in advance.
You could create a fully formed .nlogo file (it's basically a text file with a specific format), and then launch NetLogo using your Process.start command with that filename as an argument so that NetLogo will open that specific file.
You could even create a .nlogo file as a template (with whatever interface items you want), and then use string search/replace to substitute in the code that you want in the code tab.
Alternatively, fancier things are possible with the Controlling API , but I don't know much about calling JVM code from within Unity, and I suspect that will be a bigger headache than you want... unless you really need a more tight-knit connection to NetLogo, or unless the performance overhead of starting a new NetLogo process each time is unacceptable.

GhostScript printing Parameters

I am having big problems trying to print a PDF file in Windows using Ghostscript. The 'in Windows' argument comes from the fact that I am trying to use MS Windows default driver for this '-sDEVICE=mswinpr2'. I need all windows printers/drivers support. Also I can not use the PDF to images then to print job kind of solution. I cant use the gswin64c.exe file also, and I need that the job is done without any popups (no form of any kind). All I can do is just to send some parameters to gsdll32.dll and it to create a print job.
I am using C# wrapper
https://github.com/mephraim/ghostscriptsharp/tree/master
I am sending the following parameters: "-dBATCH -dNOPAUSE -dNOPROMPT -dDEVICEWIDTHPOINTS=612 -dDEVICEHEIGHTPOINTS=792 -dFIXEDMEDIA -dPDFFitPage -sDEVICE=mswinpr2 -dQUIET -sOutputFile=\"%printer%Epson Stylus Pro 4900\" D:\1.pdf"
And every time the printer selection dialog keeps popping up. I understand that the order the parameters are in matters .... because I changed it and it had different results.
Actual Question:
What parameters do I have to send to GhostScript dll so that I can print a PDF file using the default MS Windows printing driver.
Have you tried this using the command line version of GS instead of the DLL or C# thingy ? I'd suggest you concentrate on getting that to work first.
What is the name of the printer (as it appears in Windows) ?
What version of Ghostscript are you using ?
Try using the command line without '-dBATCH', '-dNOPAUSE', '-dNOPROMPT', '-dQUIET'. that way if Ghostscript tries to tell you something you won't just ignore it or miss it.
If the command line works then; I see you've escaped the " characters, but not the '%', you might want to escape those, or double them up. Depending how this wrapper of yours works they might be getting read as format specifiers.
The parameters used in the command line have been verified first in the command line version gswin64c.exe (64 bit operation system) and they work fine.
I am using GS version 9.10 (latest version).
I've tried different combinations of parameters, with or without some of them ... same result ... -100 exit code (general fault with no specification of the error that caused it).
It doesn't seem to be a problem with that % character... I'll try some more things.
Thanks Ken for the help
As far as the printer dialog popup is concerned, if you replace "mswinpr2" with the a compatible device name such as ljet4, the prompts would go away. My guess is that your computer must be having more than one printer installed and hence windows prompts for you to choose one from the list.

calling C# exe from commandline using batch file

I want to create an exe file using C# which will send emails. I want to invoke this exe using batch file. Should I create a Web form application or just a class library ? Can an exe be called using batch file from command prompt?
Please suggest.
Typically, if you want to call a application from a command line, you'd create a Console Application.
This allows your command prompt to call the application and (optionally) accept input from the console (prompt) as well as write output to the console (via the Console class).
You can read the command line arguments directly from the Main routine in the Console Application, as well.
Write a console app that takes arguments on the command line. You can then call this from script (i.e. a batch file). The arguments that you pass could contain all the elements of your email - the body may be a little large and I am not sure of the limit (if there is one) for the length of string args, but as long as the body is reasonable in size you will be ok.
I prefer writing "tools" such like this as console apps (over Windows apps) because you have so much flexibility in terms of executing the app. You can invoke it directly by typing in the cmd.exe (the command line), you can write a script to call it (as you have suggested), you can write another .NET application that can call it, etc... A Windows app has just one method of invocation.
Of course. Create the application, and store the binary exe in some folder (i.e., C:\folder).
If your exe has the name "name.exe", in your batch file, write
cd C:\folder
name
This works for all types of applications, and you can access command-line data in both, but it's easiest in a console application.
You would write:
cd C:\Users\asif\Desktop\EmailSender\ConsoleApplication1\bin\Debug\
::Space here
ConsoleApplicati‌​on1
with the new line.

Running Windows Forms Application (C# .NET 4.0) from batch file with Command Line Arguments

I'm a Silverlight/ASP.NET developer trying to write my first Windows Forms application to run in the background on a server, populating our database. Eventually would like this to be a Windows service, but it's not required initially.
I need to create a batch file to execute 5 instances of this application, passing in the URL to 5 RESTful endpoints. So I published my app, which created a setup.exe. After installing it, I have an item that points to
C:\Users\mi2dev\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Microsoft\, with a .appref-ms file.
I'm not sure at this point what to do. Running:
"C:\Users\mi2dev\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Microsoft\StreamingApp.appref-ms" -"http://www.myURL.com" throws up a command window briefly, but the app doesn't run, data doesn't populate in DB.
What am I missing here?
since your application is in .exe format. And make your winform accepts command line arguments (check the main method) also make your Form ctor accepts params too. Then just launch it via cmd line just as you would other command, but here only to navigate to that dir where file exists.
In case of batch, use start command followed by program name and then arguments
It's hard to understand what is happening inside your application. You need to debug to understand what is going on there when it receives given parameters.
So I would suggest to debug an EXE. For this go to your EXE project properties, select DEBUG tab in CommandLineArguments insert your parameter string.
Run it in DEBUG and hopefully you will figure out a problem.
If after debugging it's not yet clear why it behaves in that way, come back to SO :)
Silvi if you plan to use your windows forms application from a batch file and you imagine the applicationm will behave differently in such mode than when opened witha double click, the usual approach is to parse the command line (arguments, also available in the main method as parameter) and to avoid loading the UI at all.
in fact if you have written your application properly the UI only managed the UI and does not contain the whole logic of database manipulation and data transformation.
what you could do is check inside the Main method if there are command line parameters and if you detect any of the special ones you have definded you really avoid to even call Application.Run(new Form1(...)); and start working in batch mode without user interface.
the same logic you want to use in batch mode or in UI mode can be wrapped in helper classes (often also called business managers or business logic... it depends), so that you do not have code duplication but simply UI or batch will call those classes nicely.

How to run a bat file from a C# program?

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)

Categories

Resources