When I run the package in Visual Studio the files print. When I deploy it and run it as a SQL Server job the files do not print. Any ideas? The package RUN AS is a domain admin account.
Here is the code.
ProcessStartInfo info = new ProcessStartInfo();
info.Verb = "PrintTo";
info.Arguments = "\"printername\"";
info.FileName = path;
info.CreateNoWindow = true;
info.WindowStyle = ProcessWindowStyle.Hidden;
Process p = new Process();
p.StartInfo = info;
p.Start();
p.WaitForInputIdle();
System.Threading.Thread.Sleep(3000);
if (false == p.HasExited)
p.Kill();
Related
I have developed one windows application in which i have some implemented feature now i want to implement optimize hard disk so i got to know about defrag.exe .so i wrote some code but its not working for me. can anyone what am doing wrong ?
Process p = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
p.StartInfo.Verb = "runas";
p.StartInfo.FileName =
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), "Defrag.exe");
p.StartInfo.Arguments = #"c:\ /A";
try
{
p.Start();
p.WaitForExit();
string a= p.StandardOutput.ToString();
See my comment on your previous post. That aside, you need to set a few extra parameters - working sample below. You may also need to elevate privileges in order for your scenario to work. If this is the case, then see this post.
static void Main(string[] args)
{
Process p = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
p.StartInfo.Verb = "runas";
p.StartInfo.FileName =
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), "Defrag.exe");
p.StartInfo.Arguments = #"c:\ /A";
// Additional properties set
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.UseShellExecute = false;
p.Start();
// Fixed your request for standard with ReadToEnd
string result = p.StandardOutput.ReadToEnd();
p.WaitForExit();
}
Adding another option - try the below. To use runas, you need to set StartInfo.UseShellExecute = true which means you cant redirect the standard output - would this still work for you? Another option is to run your whole program as admin How do I force my .NET application to run as administrator? - this will allow you to redirect your output and run with elevated permissions.
static void Main(string[] args)
{
Process p = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
p.StartInfo.Verb = "runas";
p.StartInfo.FileName =
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), "Defrag.exe");
p.StartInfo.Arguments = #"c:\ /A";
// Additional properties set
//p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.UseShellExecute = true;
//p.StartInfo.CreateNoWindow = true;
p.Start();
// Fixed your request for standard with ReadToEnd
//string result = p.StandardOutput.ReadToEnd();
p.WaitForExit();
}
The following code is running fine on my computer but it hangs on office computer
ProcessStartInfo info = new ProcessStartInfo();
info.Verb = "print";
info.FileName = #"c:\temp\" + filename;
info.CreateNoWindow = true;
info.WindowStyle = ProcessWindowStyle.Hidden;
Process p = new Process();
p.StartInfo = info;
p.Start();
p.WaitForInputIdle();
System.Threading.Thread.Sleep(3000);
if (false == p.CloseMainWindow())
p.Kill();
Please help
Some computers are slower then others so the process is exiting before it is finished. Increasing the thread sleep time fixed the problem.
I am going to install the my project silently using the following options from the C#.
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = false;
startInfo.UseShellExecute = true;
startInfo.FileName = tempPath + "myproject.exe";
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.Arguments = "/s /v/qn";
using (Process exeProcess = Process.Start(startInfo))
{
exeProcess.WaitForExit();
exitcode = exeProcess.ExitCode;
}
It installs my projects sucessfully.but I need to Launch the installed application after the installation. How I can Do this? Is there any Additional arguments I need to pass to Launch the product after the installation?
Thanks in Advance,
Roshil
I am installing cab file of my application using wceload.exe. When my application already exists it gives message "My application is already installed.Reinstall?" How to avoid this message? Cab file installation launches when I use
ProcessStartInfo info = new ProcessStartInfo();
info.FileName = #"\windows\wceload.exe";
info.Arguments = "\\My_Installer.cab";
Process proc = new Process();
proc.StartInfo = info;
proc.Start();
proc.WaitForExit();
When I use
info.Arguments = "\\My_Installer.cab /silent";
or
info.Arguments = "/noaskdest /noui \\My_Installer.cab";
nothing happens. What am I doing wrong?
Here is complete solution, if it will be necessary for somebody:
ProcessStartInfo info = new ProcessStartInfo();
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
Process proc = new Process();
// uninstalls application
doc.LoadXml("<wap-provisioningdoc>" +
"<characteristic type=\"UnInstall\">" +
"<characteristic type=\"MyManufactuer MyApplication\">"+
"<parm name=\"uninstall\" value=\"1\"/>" +
"</characteristic>" +
"</characteristic>" +
"</wap-provisioningdoc>");
Microsoft.WindowsMobile.Configuration.ConfigurationManager.ProcessConfiguration(doc, false);
// installs application
info.FileName = #"\windows\wceload.exe";
info.Arguments = #"\My_Installer.cab";
// start the process
proc.StartInfo = info;
proc.Start();
proc.WaitForExit();
I have a very simple batch file:
echo Text > Test.txt
This file is saved here:
R:\Testing123.bat
full UNC pathway is
\\imfile\depart$\DB\Testing123.bat
In my console application the following runs:
Process myProcess = new Process();
myProcess.StartInfo.UseShellExecute = true;
myProcess.StartInfo.WorkingDirectory = #"R:\";
myProcess.StartInfo.FileName = #"Testing123.bat";
myProcess.StartInfo.CreateNoWindow = true;
myProcess.Start();
This does not run if I use the full pathway:
Process myProcess = new Process();
myProcess.StartInfo.UseShellExecute = true ;
myProcess.StartInfo.WorkingDirectory = #"\\imfile\depart$\DB\";
myProcess.StartInfo.FileName = #"Testing123.bat";
myProcess.StartInfo.CreateNoWindow = true;
myProcess.Start();
Can I not use these UNC pathways for the WorkingDirectory property? I thought when programming it was always best practice to use these pathways?
EDIT
Using one of the suggestions I now have the following which unformtunately still doesn't work :
{
Process myP = new Process();
myP.StartInfo.UseShellExecute = false;
string myString = #"pushd \\imfile\depart$\DB";
myP.StartInfo.FileName = "cmd";
myP.StartInfo.Arguments = myString;
myP.StartInfo.CreateNoWindow = true;
myP.Start();
Process myProcess = new Process();
myProcess.StartInfo.UseShellExecute = true ;
myProcess.StartInfo.WorkingDirectory = #"\\imfile\depart$\DB";
//myProcess.StartInfo.WorkingDirectory = #"R:\";
myProcess.StartInfo.FileName = #"Testing123.bat";
myProcess.StartInfo.CreateNoWindow = true;
myProcess.Start();
}
bat-files are executed via cmd.exe, which (by default) does not allows UNC paths as "working directory". However, you may change this behavoir via registry.
[HKEY_CURRENT_USER\Software\Microsoft\Command Processor]
"DisableUNCCheck"=dword:00000001
http://support.microsoft.com/kb/156276
This information is known to me from Far Manager TechInfo Section 2.2.