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.
Related
Want to execute a exe within Application directory, in Dev system it works fine. In IIS, it is not executed, I have tried below points:
set default application pool to local system
set defualtpool, NETWORK_SERVICE, Everyone access to exe
Enabled 32 bit application to application pool
Server Version : Windows Server 2012
IIS Version: IIS 8.0
Below is my code
p.StartInfo = new ProcessStartInfo();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = System.AppDomain.CurrentDomain.BaseDirectory.ToString() + "wkhtmltopdf\\wkhtmltopdf.exe";
string arg1 = "";
arg1 = "www.google.com" + " test.pdf";
p.StartInfo.Arguments = arg1;
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
p.StartInfo.CreateNoWindow = true;
p.Start();
p.WaitForExit(60000);
p.Close();
p.Dispose();
Make sure that the location at which your pdf file is getting generated "everyone has access to it"
it seems you are trying to convert html data to pdf file on your iis server. Make sure IIS server can access the site which your are trying to convert "Check if you can access those site using IE on your IIS server as there could be proxy issues"
Consider to set the working path of your exe:
p.StartInfo.WorkingDirectory = System.AppDomain.CurrentDomain.BaseDirectory.ToString() + "wkhtmltopdf";
Also check task manager if the exe is in memory.
Sometimes when a batch got a low level error open a prompt, and IIS do not capture it, so the process is freezed in memory.
Consider also the set a log in the exe in order to understand what goes wrong.
I had similar problem. Solved moving the exe in another folder outside IIS application. This folder should have execution rights.
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;
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.
I need to run a console application for which I do not have any source code (I only have the binaries) from my WCF REST web service. Once my service is up and running (localhost, debug), I enter the following code :
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.FileName = strCommand;
p.StartInfo.Arguments = strCommandParameters;
p.Start();
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
My .exe file is found but nothing happens. I put a breakpoint after this code and it stops there so the code is executed. Whatever happens regarding the command parameters, the console application is supposed to produce at least a log.txt file in its directory, but it does not. Any ideas?
EDIT: It was working...the only difference is the log.txt file was generated in the debug directory of the running web application instead of the directory of the console application. So the relative path of the output file is relative to the server web application.
I´d guess you are hosting the WCF Service in IIS and the console application is started using the AppPool Credentials not having enough privileges. You may try to use the ProcessMonitor to monitor the Service/ConsoleApp and check for access denied messages
I am using the following code to run a batch file from C#. The following code is part of my windows service. This code works perfectly fine in Windows XP but when I deploy my windows service to Windows server 2003 OS it returns exit code 1 (failure). Does someone know what I am missing? Do I need to give some special permission to the windows service? The service is installed as a Local System service.
ProcessStartInfo psi = new ProcessStartInfo();
//specify the name and arguments to pass to the command prompt
psi.FileName = ConfigurationManager.AppSettings["BatchFilePath"];
psi.Arguments = fileName;
//Create new process and set the starting information
Process p = new Process();
p.StartInfo = psi;
//Set this so that you can tell when the process has completed
p.EnableRaisingEvents = true;
p.Start();
//wait until the process has completed
while (!p.HasExited)
{
System.Threading.Thread.Sleep(1000);
}
//check to see what the exit code was
if (p.ExitCode != 0)
{
logger.Write("Exit Code" + p.ExitCode);
}
My next set would be to try setting the service to run as the user you're logged in as when it works. That way you'll know whether it is something specific to the Network Service account that's stopping it working