I need your help to print my locally saved file without using print dialog box , i tried out many cases but failed to do so. one case is like;
var pr = new PrintDocument();
pr.PrintController = new System.Drawing.Printing.StandardPrintController();
pr.PrinterSettings = new PrinterSettings();
pr.PrinterSettings.PrintFileName = "E:\\File.docx";
pr.PrinterSettings.PrinterName = fileName.ToString();
pr.Print();
pr.Dispose();
This will start Microsoft Word and print the test.rtf while suppressing the Print dialog box. However, the path must be fully specified.
var settings = new PrinterSettings();
var startInfo = new ProcessStartInfo();
startInfo.FileName = #"C:\Program Files\Microsoft Office\Office\WINWORD.EXE";
startInfo.Arguments = #"test.rtf /q /n /mFilePrintDefault /mFileExit";
var p = Process.Start(startInfo);
Related
help me please! I've script which displays the windows activation key. When i use this:
Process scriptProc = new Process();
scriptProc.StartInfo.FileName = #"cscript";
scriptProc.StartInfo.WorkingDirectory = #"C://....";
scriptProc.StartInfo.Arguments = "//B //Nologo name_of_script";
scriptProc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
scriptProc.Start();
Nothing happens, but if i run VBS file from cd, like this:
ProcessStartInfo cmdStart = new ProcessStartInfo();
cmdStart.FileName = "cmd.exe";
cmdStart.WindowStyle = ProcessWindowStyle.Normal;
cmdStart.Arguments = #"/k cd C://.... && start wininfo.vbs";
Process.Start(cmdStart);
I get an error that the registry key cannot be opened for reading
P.S. the script works if I just run it on the desktop
I am trying to open Weka from cmd line, using C#. This is the code that I'm using. It's giving me an error for Weka.Start() line, and the error is : Win32 exception was unhandled. System cannot find the file specified. Please help me out. Thanks
ProcessStartInfo WekaStartInfo = new ProcessStartInfo(#"C:\Program Files\Weka- 3-6\java -Xmx1536m -jar weka.jar");
WekaStartInfo.UseShellExecute = false;
WekaStartInfo.RedirectStandardOutput = true;
WekaStartInfo.RedirectStandardError = true;
WekaStartInfo.CreateNoWindow = false;
Process Weka = new Process();
Weka.StartInfo = WekaStartInfo;
Weka.Start();
string output = Weka.StandardOutput.ReadToEnd();
Weka.WaitForExit();
There are two options to start WEKA from a
C# application.
In the WEKA install directory there is a
batch file called RunWeka.bat. To start WEKA
using this batch file use the following
code:
ProcessStartInfo wekaStartInfo =
new ProcessStartInfo(#"c:\Program Files\Weka-3-6\runweka.bat", "default");
wekaStartInfo.WorkingDirectory = #"c:\Program Files\Weka-3-6";
wekaStartInfo.UseShellExecute = false;
wekaStartInfo.RedirectStandardOutput = true;
wekaStartInfo.RedirectStandardError = true;
wekaStartInfo.CreateNoWindow = false;
using(Process weka = new Process())
{
weka.StartInfo = wekaStartInfo;
weka.Start();
}
To start WEKA without using the batch file
use the following code:
ProcessStartInfo wekaStartInfo =
new ProcessStartInfo(#"javaw", #"-classpath . RunWeka -i .\RunWeka.ini -w .\weka.jar -c default");
wekaStartInfo.WorkingDirectory = #"c:\Program Files\Weka-3-6";
wekaStartInfo.UseShellExecute = false;
wekaStartInfo.RedirectStandardOutput = true;
wekaStartInfo.RedirectStandardError = true;
wekaStartInfo.CreateNoWindow = false;
using(Process weka = new Process())
{
weka.StartInfo = wekaStartInfo;
weka.Start();
}
In both cases you have to set the working directory.
You've probably specified incorrect or inexistent location for your process based on the error description. Check that the path specified in ProcessStartInfo is correct.
Maybe, there are unnecessary spaces in the declaration here:
ProcessStartInfo WekaStartInfo = new ProcessStartInfo(#"C:\Program Files\Weka-3-6\java -Xmx1536m -jar weka.jar");
In the constructor of ProcessStartInfo you must either enter just the application name, or specify the arguments separate;
ProcessStartInfo WekaStartInfo = new ProcessStartInfo(
#"C:\Program Files\Weka-3-6\java.exe",
#"-Xmx1536m -jar weka.jar");
I have following code
using (StreamWriter outfile = new StreamWriter(#"f:\trial.cpp"))
{
outfile.Write(txtCode.InnerText);
}
System.Diagnostics.ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo(#"cl.exe", #" 'trial.cpp'");
procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = false;
procStartInfo.UserName = "asdasd";
SecureString secureString = new SecureString();
foreach (char c in "abcded")
{
secureString.AppendChar(c);
}
procStartInfo.Password = secureString;
// Do not create the black window.
procStartInfo.CreateNoWindow = true;
procStartInfo.WorkingDirectory = #"f:\";
// Now we create a process, assign its ProcessStartInfo and start it
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo = procStartInfo;
proc.Start();
// Get the output into a string
string result = proc.StandardOutput.ReadToEnd();
How to pass file name as parameter? Above code doesn't run and I have tried all full path, different path options.
can anyone help?
The argument is set incorrectly. You have:
var procStartInfo = new ProcessStartInfo(#"cl.exe", #" 'trial.cpp'");
Where there are spaces and single quotes in the name. Try:
var procStartInfo = new ProcessStartInfo(#"cl.exe", #"trial.cpp");
EDIT:
startInfo.CreateNoWindow = false;
startInfo.UseShellExecute = false;
startInfo.FileName = "CL.exe";
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.Arguments = "trial.cpp";
try
{
using (Process exeProcess = Process.Start(startInfo))
{
exeProcess.WaitForExit();
}
}
catch
{
// error handling
}
The point here is that CL is a command line executable, not a windows GUI application.
http://msdn.microsoft.com/en-us/library/f2ccy3wt.aspx
http://msdn.microsoft.com/en-us/library/kezkeayy.aspx
http://msdn.microsoft.com/en-us/library/9s7c9wdw.aspx
If the cl.exe is not in the system PATH (which by default it is not) then the start process will not find the executable and it will fail to run.
So I suspect you are seeing the fact that the cl.exe is not in the system PATH.
I have a problem with selecting printer to print my document.
My code is :
var filename = #"C:\Users\I\Desktop\test.doc";
PrintDialog pd = new PrintDialog();
pd.PrinterSettings =new PrinterSettings();
if (DialogResult.OK == pd.ShowDialog(this))
{
Process objP = new Process();
objP.StartInfo.FileName = filename;
objP.StartInfo.WindowStyle =
ProcessWindowStyle.Hidden; //Hide the window.
objP.StartInfo.Verb ="print";
objP.StartInfo.Arguments ="/p /h \"" + filename + "\" \"" + pd.PrinterSettings.PrinterName + "\"";
objP.StartInfo.CreateNoWindow = false;
//true;//!! Don't create a Window.
objP.Start();
//!! Start the process !!//
objP.CloseMainWindow();
}
and whatever I choose, process always will use default printer, no matter what value of pd.PrinterSettings.PrinterName is.
What's wrong with my code?
You probably want to use "PrintTo" instead of "print" for the verb. You already set objP.FileName to the filename so there's no need to get complicated in the arguments. Pass the printer name alone there.
var filename = #"C:\Users\I\Desktop\test.doc";
PrintDialog pd = new PrintDialog();
pd.PrinterSettings =new PrinterSettings();
if (DialogResult.OK == pd.ShowDialog(this))
{
Process objP = new Process();
objP.StartInfo.FileName = filename;
objP.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; //Hide the window.
objP.StartInfo.Verb ="PrintTo";
objP.StartInfo.Arguments = pd.PrinterSettings.PrinterName;
objP.StartInfo.CreateNoWindow = false;
//true;//!! Don't create a Window.
objP.Start();
//!! Start the process !!//
objP.CloseMainWindow();
}
Try changing pd.PrinterSettings =new PrinterSettings(); to read something like this:
pd.PrinterSettings =new System.Drawing.Printing.PrinterSettings;
By default when you create an instance of printer settings it returns the default printer name just an fyi... you can then try something like this
//sudu code
foreach(string strPrinter in PrinterSettings.InstalledPrinters)
{
// or unless you know the name of the printer then skip this and assign it to the code above
}
How can I launch the print of a document from C# .NET application ?
the Word document already exists in the hard drive. I just wish to start printing that Word document upon the button click event.
ProcessStartInfo psi = new ProcessStartInfo(wordFilename)
{
UseShellExecute = true,
Verb = "print",
RedirectStandardOutput = false,
CreateNoWindow = true
};
using (Process p = new Process {StartInfo = psi})
{
p.Start();
p.WaitForExit();
}
To do this kind of thing you need to know about System.Diagnostics.Process , the MSDN page shows how to pridnt a Word document as an example. A short version:
System.Diagnostics.Process printProcess = new System.Diagnostics.Process();
printProcess.StartInfo.FileName = #"X:\test\print this.doc";
printProcess.StartInfo.Verb = "Print";
printProcess.StartInfo.CreateNoWindow = true;
printProcess.Start();