I'm trying to uploading a file and then using server-side processes to convert it.
This is part of a Visual Studio Web ASP.NET web application running on a ASP.NET Development server, localhost:8638
string fn = System.IO.Path.GetFileNameWithoutExtension(File1.PostedFile.FileName);
Process p = new Process();
p.StartInfo.WorkingDirectory = Server.MapPath("/Data");
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = "soffice --headless --invisible -convert-to pdf "+fn+".ppt";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.Start();
p.WaitForExit();
I can manually open cmd.exe inside of the Datadirectory, then type this command, substituting the file name, and it'll work. However, running this code does not produce any result
What am I missing, or doing wrong?
You can't just pass everything in to cmd. You need to use the /C parameter, which will open a command prompt with those commands and terminate it when it finishes running that command. Try changing your arguments to
StartInfo.Arguments = "/C soffice --headless --invisible -convert-to pdf "+fn+".ppt";
An alternate solution would be to simply run the process itself (as suggested in the comments by SLaks). Change p.StartInfo.FileName to the appropriate executable, edit your arguments, and you should be good to go. That should be the preferred method, as it does what you want more directly.
Related
I have to design an API which will first pull the latest code from the repository and then execute some node js command on that code. I execute this using command prompt through c# code.
Code sample:
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.WorkingDirectory = Path;
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = "/c git pull";
p.Start();
p.WaitForExit();
p.StartInfo.Arguments = "/c node populate";
p.Start();
Everything work perfect on my local machine. But when I deployed this on the server only the git pull command not working, rest other commands working perfectly.
Even another git command like - git status and git rev-parse --short HEAD do work correctly but the problem seems with git pull.
I also tried git pull https://user:pass#domain/repo but no luck.
When I execute git pull command on the server manually through command prompt it works.
If it is due to IIS user permission than what setting are required.
Any guidance would be highly appreciated.
I am calling an exe program, when invoked requires inputfile.txt and its generates outputfile.txt in same folder. exe is working fine.
Now I want to convert same thing in web application but there I am getting issues.
Code I am using is as below
// Prepare the process to run
ProcessStartInfo start = new ProcessStartInfo();
// Enter in the command line arguments, everything you would enter after the executable name itself
start.Arguments = #"C:\Program Files\AET_Calculator\application\InputFile.txt"; ;
// Enter the executable to run, including the complete path
start.FileName = #"C:\Program Files\AET_Calculator\application\AET_Calculator.exe";
// Do you want to show a console window?
start.WindowStyle = ProcessWindowStyle.Hidden;
start.Verb = "runas";
start.UseShellExecute = true;
int exitCode;
// Run the external process & wait for it to finish
using (Process proc = Process.Start(start))
{
proc.WaitForExit();
// Retrieve the app's exit code
exitCode = proc.ExitCode;
}
Its throwing error. How can I call exe from web application passing input txt file
Here is error I am getting :
You need to grant execute access on the .exe file to the user in IIS is going to use to access the file, most of the time app pool identity or in IIS the IIS_WPG user.
I have the below command which run batch file, I need to run this batch when I open the ASPX page in the machine browser to affect the machine and not affect server :
ProcessStartInfo psi = new ProcessStartInfo(this.WhiteLabel.Text);
psi.RedirectStandardOutput = false;
psi.WindowStyle = ProcessWindowStyle.Hidden;
psi.UseShellExecute = true;
Process.Start(psi);
This is not possible because of security concerns. The only possible way might be to create an ActiveX library that the user would acknowledge and accept on their browser to run. What are you trying to run on the client? Maybe there is another approach?
This has been asked before see here...
How to execute an application on the client from a website?
I am trying to run a jar in C# by running this code:
System.Diagnostics.ProcessStartInfo processInfo = new System.Diagnostics.ProcessStartInfo(pathForjre+"java.exe", "-jar "+jar+" "+argsforjar);
processInfo.CreateNoWindow = true;
processInfo.UseShellExecute = false;
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo = processInfo;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.RedirectStandardError = false;
string out = "";
proc.Start();
proc.WaitForExit();
while (!proc.StandardOutput.EndOfStream)
out += proc.StandardOutput.ReadLine();
proc.Close();
return out;
When the jar runs I get an access denied exception with the path the jar is trying to write the log file to. I can manually run the jar from the command line and the log file writes no problem. How do I give the process permission to do things like write a file?
ASP.NET apps by default don't impersonate the identity of the user that is hitting the site. Typically then run as the local Network Service account unless you specifically have the process run under a different identity (you can determine the identity it's using by looking at Environment.UserName).
You can either set up the site (or use impersonation) to run under an identity that has permission to write to that location, or give write permision to the account that it's currently using.
Setting the working directory of the process resolved the issue.
proc.StartInfo.WorkingDirectory = path;
SHORT VERSION
How do you figure out which DLL is failing to load (and potentially why) when a process exits with error code -1073741502?
LONG VERSION
I'm trying to write a pretxnchangegroup hook for Mercurial, and as a part of that hook I need to get the output of running the command:
hg log
The code that I'm using to start and run the hg.exe process is as follows:
string Command = "log";
Process p = new Process();
ProcessStartInfo psi = p.StartInfo;
p.StartInfo.FileName = #"C:\Program Files (x86)\Mercurial\hg.exe";
psi.CreateNoWindow = true;
psi.LoadUserProfile = true;
psi.RedirectStandardError = true;
psi.RedirectStandardOutput = true;
psi.UseShellExecute = false;
psi.WorkingDirectory = Environment.CurrentDirectory;
p.StartInfo.Arguments = Command;
// Pass-through environment variables
psi.UserName = Properties.Settings.Default.HG_User;
psi.Domain = Properties.Settings.Default.HG_Domain;
psi.Password = new System.Security.SecureString();
foreach (char c in Properties.Settings.Default.HG_Pass)
{
psi.Password.AppendChar(c);
}
p.Start();
p.WaitForExit();
The problem is that the process keeps exiting with error code -1073741502, without outputting anything on Standard Output or Standard Error. After some research online, I discovered that this error code has something to do with the application failing to initialize properly (couldn't find DLL's, maybe?), but I have no idea how to go about figuring out how to fix it.
Keep in mind that this hook is being called for when I'm pushing to the repository over the web (so, IIS is calling the Mercurial CGI via Python, which has this program configured as a hook).
In a totally different web application, I'm able to run HG commands just fine, and I'm also able to run this by doing
runas /user:<same account as in the code> /noprofile cmd.exe and then manually typing in the hg command line.
Also, if I set UseShellExecute = true, then it executes just fine, but then I can't get the Standard Output. I'm really tempted to just make a web service call to the web app which is able to execute this command successfully, but that'd be a really ugly solution.
Any ideas why this thing isn't executing?
I was able to resolve this by disabling UAC so it sounds like a permissions problem even though I do not know the exact details.