Printing filepath settings its printer and tray as argument - c#

I try to print file for example C:/exmaple.docx but I need to specify for it printer and tray which I get from print dialog. I do not now how to set tray (paper source) as argument. Setting printer as argument works. This is my code:
System.Diagnostics.ProcessStartInfo info = new System.Diagnostics.ProcessStartInfo();
info.Arguments = "\"" + somePrinterName + "\"";
info.Verb = "C:\\example.docx";
info.FileName = "C:\\example.docx";
info.UseShellExecute = true;
info.CreateNoWindow = true;
info.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo = info;
p.Start();

I don't believe there are printer tray options in the System.Diagnostics namespace, but for Word documents you can print from a specific tray by using the Word Interop library (Microsoft.Office.Interop.Word).
That'd let you do something like
wordDocument.PageSetup.FirstPageTray = Word.WdPaperTray.wdPrinterUpperBin;
wordDocument.PageSetup.OtherPagesTray = Word.WdPaperTray.wdPrinterLowerBin;
There's a more fleshed-out example here (that developer has tray selection working, but is struggling with duplex printing).
See also:
MSDN Reference for WdPaperTray enum
MSDN Social: Printing Options C#

Related

Unity C# Encoding Type

I create simple unity application, there is button that give user print a picture. When button is clicked, it will run a command in cmd but I get this message,
'cd' is not recognized as an internal or external command,
operable program or batch file.
I search the web, it says I need to change encoding type but I have do it in Notepad++ (I change it from UTF-8 BOM to ANSI) but it still no work.
Here is my code, in case I am wrong.
string fullCommand = "rundll32 C:/WINDOWS/system32/shimgvw.dll,ImageView_PrintTo " + filePath + printerName
System.Diagnostics.Process process = new System.Diagnostics.Process();
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.CreateNoWindow = false;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.UseShellExecute = false;
process.StartInfo.Arguments = "/c " + fullCommand;
process.Start();
This is my reference
How to direct printing of photo or text using Unity without preview
I expect it to print the expected file but it do not. I hope someone can guide me. Thank you for helping.
Cheers
Kev

How do I get the output from handbrakecli --scan?

I am working on a c# program to loop over my Windows Media Center recorded TV shows (.wtv) and convert them using the handbrake cli. I just got everything to work now and I wanted to also utilize the --scan function so that I can customize the audio and video arguments based on the input file rather then set a static.
This is what I have so far for the scan but I can't seem to find where the data is that prints out to the console window.
var p = new Process();
var pSI = new ProcessStartInfo();
pSI.RedirectStandardOutput = true;
pSI.UseShellExecute = false;
pSI.FileName = HandBrakeLocation;
pSI.Arguments = string.Concat(#"--scan -i ", '"', inputFile, '"');
pSI.WindowStyle = ProcessWindowStyle.Hidden;
p.StartInfo = pSI;
p.Start();
var stdout = p.StandardOutput;//streamreader
p.WaitForExit();
I thought that perhaps the p.StandardOutput would send the console output to the stdout StreamReader variable, but I could not find it anywhere inside the object. What am i missing?
Thanks for you time and assistance.
You can read the program output using the StandardOutput property of the process (which is a stream):
var output = stdout.ReadToEnd();
p.WaitForExit();
More info can be found on MSDN: https://msdn.microsoft.com/en-us/library/system.diagnostics.process.standardoutput%28v=vs.110%29.aspx

Run cmd command from inside my application

I would like to make an application using C# form. Form has multiple label to show information and a button to click and show that information on that labels. All of the labels will show information which can be found using cmd. cmd will not show when program executes.
For Example:
If I need my motherboard information. It can be done using cmd commands "wmic baseboard get product,manufacturer"(without quotes). I would like to show same information on my C# form label by clicking on button. That need to be done hiding cmd windows.
You can use ProcessStartInfo and Process classes to run this application and redirect standard output to your own method.
Setting RedirectStandardOutput will make Process raise OutputDataReceived event which you can easily handle.
P.S. Pay attention to using Arguments to provide arguments.
var psi = new ProcessStartInfo(#"wmic");
psi.Arguments = #"baseboard get product,manufacturer";
psi.WindowStyle = ProcessWindowStyle.Hidden;
psi.UseShellExecute = false;
psi.RedirectStandardOutput = true;
string val = String.Empty;
var p = Process.Start(psi);
p.BeginOutputReadLine();
p.OutputDataReceived += delegate(object sender, DataReceivedEventArgs eventArgs)
{
val += eventArgs.Data + "\r\n";
};
p.WaitForExit();
MessageBox.Show(val); // Start parsing it here
Reference the code below against http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo%28v=vs.110%29.aspx and http://msdn.microsoft.com/en-us/library/system.diagnostics.process(v=vs.110).aspx
System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo(yourCmd, yourCmdArguments);
psi.RedirectStandardOutput = false;
psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
psi.UseShellExecute = true;
System.Diagnostics.Process externalProcess;
externalProcess = System.Diagnostics.Process.Start(psi);
externalProcess.WaitForExit();

Running Phantomjs using C# to grab snapshot of webpage

I'm trying to grab snapshots of my own website using phantomjs - basically, this is to create a "preview image" of user-submitted content.
I've installed phantomjs on the server and have confirmed that running it from the command line against the appropriate pages works fine. However, when I try running it from the website, it does not appear to do anything. I have confirmed that the code is being called, that phantom is actually running (I've monitored the processes, and can see it appear in the process list when I call it) - however, no image is being generated.
I'm not sure where I should be looking to figure out why it won't create the images - any suggestions? The relevant code block is below:
string arguments = "/c rasterize.js http://www.mysite.com/viewcontent.aspx?id=123";
string imagefilename = #"C:\inetpub\vhosts\mysite.com\httpdocs\Uploads\img123.png";
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.CreateNoWindow = false;
p.StartInfo.FileName = #"C:\phantomjs.exe";
p.StartInfo.Arguments = arguments + " " + imagefilename;
p.Start();
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
I check the errors that phantomjs throws during its process.
You can read them from Process.StandardError.
var startInfo = new ProcessStartInfo();
//some other parameters here
...
startInfo.RedirectStandardError = true;
var p = new Process();
p.StartInfo = startInfo;
p.Start();
p.WaitForExit(timeToExit);
//Read the Error:
string error = p.StandardError.ReadToEnd();
It will give you an idea of what happened
The easiest way for executing phantomjs from C# code is using wrapper like NReco.PhantomJS. The following example illustrates how to use it for rasterize.js:
var phantomJS = new PhantomJS();
phantomJS.Run( "rasterize.js", new[] { "https://www.google.com", outFile} );
Wrapper API has events for stdout and stderr; also it can provide input from C# Stream and read stdout result into C# stream.

How to bring up the "Windows cannot open this file" dialog?

My users can attach documents to various entities in the application. Of course, if user A attaches a .TIFF file, user B may not have a viewer for that type of file.
So I'd like to be able to bring up this dialog:
alt text http://www.angryhacker.com/toys/cannotopen.png
My application is C# with VS2005.
Currently I do Process.Start and pass in the file name. If no association is found, it throws an exception.
Process pr = new Process();
pr.StartInfo.FileName = fileTempPath;
pr.StartInfo.ErrorDialog = true; // important
pr.Start();
This should do it:
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.FileName = "rundll32.exe";
p.StartInfo.Arguments = "shell32.dll,OpenAs_RunDLL " + yourFileFullnameHere;
p.Start();

Categories