I tried to print a pdf file from my windows service.It didnt work.Later i wrote a console app to print a pdf file.Console app did work!.Afterwords i i called that console app from service to print pdf file it didn'work. Why is that "print" doesnt work with windows service? following are the code snippets i tried
1.Used adobe reader:
PdfReportGeneration.Log logs = new PdfReportGeneration.Log();
logs.writeLog("PrintDocument filepath:-" + filepath);
Process process = new Process();
process.StartInfo.FileName = filepath;
process.StartInfo.UseShellExecute = true;
process.StartInfo.Verb = "printTo";
process.StartInfo.Arguments = "HP LaserJet P1005";
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
process.Start();
process.WaitForInputIdle();
process.Kill();
2. Used foxit reader/adobe reader both didnt work
string sArgs = " /t \"" + filepath + "\" \"" + "HP LaserJet P1005" + "\"";
System.Diagnostics.ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = #"C:\Program Files\Foxit Software\Foxit Reader\Foxit Reader.exe";
startInfo.Verb = "printTo";
startInfo.Arguments = sArgs;
startInfo.CreateNoWindow = true;
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
System.Diagnostics.Process proc = Process.Start(startInfo);
proc.WaitForExit(100000); // Wait a maximum of 10 sec for the process to finish
if (!proc.HasExited)
{
proc.Kill();
proc.Dispose();
// return false;
}*/
Done a lots of google bing yahoo.. no use!!
Service is usually run by different account. I would try to run service as user. Could be a problem that system user doesn't have mapped that printer. Service install class would look like this:
[RunInstaller(true)]
public class ServiceInstall : Installer
{
public ServiceInstall()
{
ServiceInstaller serviceInstaller = new ServiceInstaller();
ServiceProcessInstaller serviceProcessInstaller = new ServiceProcessInstaller();
serviceProcessInstaller.Account = ServiceAccount.User;
serviceProcessInstaller.Username = "User";
serviceProcessInstaller.Password = "Password";
serviceInstaller.DisplayName = "Some Service";
serviceInstaller.StartType = ServiceStartMode.Automatic;
serviceInstaller.ServiceName = "Some Service";
this.Installers.Add(serviceProcessInstaller);
this.Installers.Add(serviceInstaller);
}
}
Related
Use C# to copy WINDOWS log files to other disks. The main problem is that the CMD command with administrator privileges will not be called, and some of the things mentioned on the Internet will not work.
private Process proc = null;
public Command()
{
proc = new Process();
}
public void RunCmd(string cmd)
{
proc.StartInfo.CreateNoWindow = true;
proc.StartInfo.FileName = "cmd.exe";
proc.StartInfo.Arguments = "C:\Windows\System32\cmd.exe";
proc.StartInfo.UseShellExecute = true;
proc.StartInfo.RedirectStandardError = true;
proc.StartInfo.RedirectStandardInput = true;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.Verb = "RunAs";
proc.Start();
proc.StandardInput.WriteLine(cmd);
proc.Close();
}
public void ChangeFile(string path1,string path2)
{
Command cmd = new Command();
cmd.RunCmd("attrib -s" + " " + path1);
Directory.CreateDirectory(path2);
cmd.RunCmd("Xcopy" + " " + path1 + " " + path2);
}
Hope someone can tell me how to fix it.
You can restart a process as an administrator like this:
ProcessStartInfo info = new ProcessStartInfo(#"C:\Windows\cmd.exe");
info.UseShellExecute = true;
info.Verb = "runas";
Process.Start(info);
For details, please refer to Run process as administrator from a non-admin application
I am trying to restart iis remotely (Windows Servr 2012) from my local machine (Windows 7). The below command in command line doesn't work to restart IIS;
iisreset servername /restart
but the below command works fine when I tried in command line.
psexec iisreset \\servername /restart
Now the issue is when I try with below code in C#,
Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "\C psexec iisreset \\servername /restart";
startInfo.RedirectStandardOutput = true;
startInfo.UseShellExecute = false;
process.StartInfo = startInfo;
process.Start();
// capture what is generated in command prompt
var output = process.StandardOutput.ReadToEnd();
If I give any other arguments in the above code like 'ipconfig', it gives me the expected output. But when I try with psexec, it gives empty output. But it works well when tried in command prompt.
I have also tried by using 'psexec.exe' in the filename and by removing '\C psexec' in the arguments. But still no luck.
Could you please anyone help me to resolve this?
Thanks in Advance.
I have found that when using PSexec like this that you Shouldn't use CMD.exe, and you need to ensure that you have the full path to psexec. Even if it is in the same directory as your application exe.
//Assume that psexec.exe is in same location as application
//Get directory of running applications
String AppPath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().CodeBase).Replace("file:\\","");
//Set up start infor details
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.UseShellExecute = false;
//Combine path of running app
startInfo.FileName = Path.Combine(AppPath, "psexec.exe");
startInfo.Arguments = "\\servername c:\path\to\iisreset /restart";
//Execute
Process nProc = Process.Start(startInfo);
nProc.Start();
I hope u need to provide the domain admin credentials to it.
private int ExcecuteCommand(string command, string fileName, bool getResult, string timeout = null)
{
try
{
var secure = new SecureString();
foreach (char c in txtAdminPassword.Text)
{
secure.AppendChar(c);
}
System.Diagnostics.Process pProcess = new System.Diagnostics.Process();
pProcess.StartInfo.Domain = txtDomainName.Text;
pProcess.StartInfo.UserName = txtUser.Text;
pProcess.StartInfo.Password = secure;
pProcess.StartInfo.FileName = fileName;// AppDomain.CurrentDomain.BaseDirectory + #"PSTools\PsExec.exe"; ;
//pProcess.StartInfo.Arguments = string.Format(#"\\{0} -i -s -accepteula ipconfig /all", ipAddress);
//pProcess.StartInfo.Arguments = string.Format(#"\\{0} -accepteula netstat -ano",ipAddress);
//pProcess.StartInfo.Arguments = string.Format(#"\\{0} -accepteula -i CheckURLConnectivity", ipAddress);
//pProcess.StartInfo.Arguments = string.Format(#"\\{0} -accepteula ping {2}", ipAddress, AppDomain.CurrentDomain.BaseDirectory + #"Installer\CheckURLConnectivity.exe","10.10.10.35");
//pProcess.StartInfo.Arguments = string.Format(#"\\{0} -accepteula cmd /c type C:\ServiceLog.txt", ipAddress);
pProcess.StartInfo.Arguments = command;//string.Format(#"\\{0} -accepteula -c -f {1}", compName, AppDomain.CurrentDomain.BaseDirectory + #"Installer\CheckURLConnectivity.exe");
pProcess.StartInfo.UseShellExecute = false;
Process.StartInfo.RedirectStandardInput = true;
pProcess.StartInfo.RedirectStandardOutput = true;
pProcess.StartInfo.RedirectStandardError = true;
pProcess.StartInfo.CreateNoWindow = true;
logger.log("Query " + command);
pProcess.Start();
if (timeout == null)
pProcess.WaitForExit();
else
pProcess.WaitForExit(Convert.ToInt32(timeout));
string strOutput = string.Empty;
if (pProcess.HasExited == true && pProcess.ExitCode != 0)
{
string _errorMessage = GetWin32ErrorMessage(pProcess.ExitCode);
pProcess.Kill();
return pProcess.ExitCode;
}
else
return 0;
}
catch (Exception)
{
return -1;
}
}
IISreset requires elevated privilege to work.So you have to use the -h switch with psexec
-h If the target system is Vista or higher, has the process run with the account's elevated token, if available.
Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.FileName = "psexec.exe";
startInfo.Arguments = "-h iisreset \\servername /restart";
startInfo.RedirectStandardOutput = true;
startInfo.UseShellExecute = false;
process.StartInfo = startInfo;
process.Start();
// capture what is generated in command prompt
var output = process.StandardOutput.ReadToEnd();
Thank you all for your valuable response. It works fine with below code :)
startInfo.FileName = #"C:\Windows\Sysnative\PsExec.exe";
startInfo.Arguments = "iisreset \\servername /restart";
Reference: Process.Start in C# The system cannot find the file specified error
I use this code to install in silent mode a program :
private void Install_Click(object sender, EventArgs e)
{
string path = AppDomain.CurrentDomain.BaseDirectory.ToString();
string configfilename = path + "config.ini";
string installerfilename = path + "installer.ini";
string configtext = File.ReadAllText(configfilename);
string installertext = File.ReadAllText(installerfilename);
}
Process process = new Process();
process.StartInfo.FileName = #"D:\Matlab\NSIS\R2008a\win64\setup.exe";
process.StartInfo.Arguments = "/quiet";
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
process.Start();
process.WaitForExit();
}
But this code made setup.exe to run, but doesn't use the installer.ini where I have the license number, outlog number ...How I can do this, to use the arguments of installer.ini for a silent installer of Matlab program ?
Also I tried this :
Process process = new Process();
process.StartInfo.FileName = #"D:\Matlab\NSIS\R2008a\win64\setup.exe";
process.StartInfo.Arguments = #"C:\temp\installer.ini";
process.StartInfo.Arguments = "/quiet";
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
process.Start();
process.WaitForExit();
As far as I understood
http://www.mathworks.com/matlabcentral/answers/106140-how-do-i-utilize-silent-activation-using-activate-ini
http://www.mathworks.com/help/install/ug/install-noninteractively-silent-installation.html
the installer wants -if key and installer.ini file, so the C# syntax will be
// Put IDisposable into using
using (Process process = new Process()) {
// The installer itself
process.StartInfo.FileName = #"D:\Matlab\NSIS\R2008a\win64\setup.exe";
// suggested (see links above) arguments
process.StartInfo.Arguments = #"-if C:\temp\installer.ini";
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
process.Start();
process.WaitForExit();
}
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.