i need to launch simple console application from my asp.net webpage, that creates a file using given parameters. hereis my code:
string filePath = Server.MapPath(#"~/Patcher/TestPatcher.exe");
System.Diagnostics.ProcessStartInfo info = new System.Diagnostics.ProcessStartInfo(filePath , "-sn:123456789123456 -upd -fn:test.hd");
info.UseShellExecute = false;
info.RedirectStandardOutput = true;
System.Diagnostics.Process p = System.Diagnostics.Process.Start(info);
p.WaitForExit(25000);
// Display the output
string str = p.StandardOutput.ReadToEnd();
problem is that program doesnt create any file, but it does launch - i got some output in "str". any suggestions? and btw, that website must work on shared hosting.
Yeah. I think you have a security issue there... you can't just run an exe from a sandboxed environment (which the browser is).
Besides the code snippet you provided, is trying to run code on the webserver, not on from the asp.net page.
Related
I have A requirement to run SAS script through Web application using asp.net and c#.
I have used ProcessStartInfo to execute SAS script. This works fine locally with solution.Once i hosted the application in IIS, it is not working and returning exit code with 111. Please help me to solve this issue.
ProcessStartInfo info = new ProcessStartInfo("path of SAS EXE","file path");
int exitCode = 0;
info.RedirectStandardOutput = true;
info.UseShellExecute = false;
Process p = Process.Start(info);
p.WaitForExit();
exitCode = p.ExitCode;
Have you considered not doing it using process? SAS has a technology called Integration Technologies which you most likely have. It interfaces using a COM interface. You can then execute SAS that way and pass what is needed vs using a command line call.
Here is sample code:
SAS.Workspace ws = new Workspace();
LanguageService ls = ws.LanguageService;
StoredProcessService sp = ls.StoredProcessService;
sp.Repository = #"file:" + #"x:\temp";
sp.Execute("test.sas", string.Empty);
string log = ls.FlushLog(1000);
If you need to do it via the process start, here is code:
Also, if you are passing commands to SAS, I don't see any. You have to pass commands to SAS from command line (program name at a minimum). info.Arguments is a start. Also, redirect the std output to a file. Look at info.RedirectStandardOutput and info.RedirectStandardError. However, i don't believe that is the issue. I think you are encountering a security issue. Look at Event Viewer and see if it pops up. IISS requires security to execute in a directory.
Finally, why are you using IIS? Unless you have a legacy requirement, IIS should not be used. Switch to Kestrel and ASP.NET Core. I will be presenting a paper at SGF on the use of SAS in this way. Download the paper and code as soon as they are available (next week?)
I'm using Pdf2Text in an ASP.NET web app. The web interface allows PDF files to be uploaded and converted to text. To convert to text, I use the C# function below, which relies on running the Pdf2Text program via the Process library.
void ExtractOCR(string input, string output)
{
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = true;
startInfo.UseShellExecute = true;
startInfo.FileName = Server.MapPath("ocr/Pdf2Text.exe");
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.Arguments = input + " " + output;
Process exeProcess;
using (exeProcess = Process.Start(startInfo))
{
exeProcess.WaitForExit();
}
}
I've double-checked that the input and output paths are all valid. However, when I run the web app, I get the following error.
I've tried the Just-In-Time debugger but it won't even run for some reason. The Pdf2Text is a precompiled file, I don't have it's source code. I believe this is the file's download site, but not 100% sure. I've checked online to find solutions to similar errors but none has worked.
Thank you #GraDea for suggesting to look into the pool's permissions into the web app directory, this was the solution.
The web app was located at a custom location (not the default ASP server directory of inetpub\wwwroot). After the suggestion, I tried adding the pool user to the web app's custom location, but that didn't seem to work. Next, I moved the web app to the inetpub\wwwroot location and added the pool user to the folder, restarted the site via IIS and everything is now back to normal.
For future reference to anyone, easiest fix to a similar problem is to make sure your web app is in the default IIS server directory, and that your site pool's username is added to the application folder's security permissions. I've not tried the fix for a custom location because it's not so important for me, but I'm guessing it will most likely also involve adding the necessary IIS default pool users (e.g. IIS_IUSRS) and the site pool's user.
I have an exe file of a console application that takes parameter as:
A.exe --i=123 --o=abc
The out is a generic tsv file created at a predefined output folder.
For some reason I need to develop a web application which can call A.exe (such that input parameters are passed as request parameters) and return the content of the file as a web response.
Something like:
www.myserver.com/A?i=123?o=abc
Can someone suggests few pointers to get started?
You can try using Process as follows,
Process proc = new Process();
proc.EnableRaisingEvents = false;
proc.StartInfo.FileName = Path.Combine(exePath, #"A.exe");
proc.StartInfo.Arguments = String.Format(#"-i = ""{0}"" -o = ""{1}""", "123", "abc");
proc.Start()
Note : Set proper permission to the application pool to run this application.
Make a dll of your exe and call it from asp.net handler .ashx
It is a better option.
I have a question regarding launching an external application from an asp.net website. Now I think there are security measures in place to prevent that and I know it's not a good practice, but for an intranet site I'm developing it would be incredibly handy to be able to do that.
So far I've found where I can launch an external app if I'm running my server locally (in debug) then it works fine. When I publish the files out to my webserver it no longer works. I know my filepaths are going to be different I imagine, but how can I check if a local file exists? Or can I?
So here's my code:
//the actual launch button on the page
protected void btnLaunchTnet_Click(object sender, EventArgs e)
{
string tnetpath = "c:\path\tnet.exe";
RunProcess(tnetpath, "");
}
private void RunProcess(string cmd, string arguments)
{
System.Diagnostics.Process p;
p = new System.Diagnostics.Process();
p.StartInfo.FileName = cmd;
if (arguments.Length > 1)
{
p.StartInfo.Arguments = arguments;
}
p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
p.StartInfo.RedirectStandardOutput = false;
p.StartInfo.UseShellExecute = false;
p.Start();
}
I also have a quick 'check if this file exists' that I use to disable or enable the button that launches the app
private bool CheckFileExists(string filepath)
{
FileInfo SetupPath = new FileInfo(filepath);
return SetupPath.Exists;
}
I know of a way using ActiveX controls, but then it would work only with Internet Explorer.
Check out this post.
It sounds like you want to launch a program on the client machine.
You can't do that.
If you're on the same domain and using Windows Authentication, you should be able to use impersonation and SysInternals PsExec.exe to launch the process remotely.
I'm only writing what #Slaks wrote but using more words.
When you browse to a web page the following things happen.
The web browser program makes a network connection to the web server program.
The web browser sends an HTTP request to the web server.
The web server processes the information in the HTTP request.
The web server sends an HTTP response back to the browser.
The browser program displays the HTML contained in the HTTP response.
The code in your example is code that will run in step 3 above.
This should be enough information for you to see why you can't launch a process in the way that you want to, but let me know if you don't see it and I'll explain more.
i m building a website, and i want to run MATLAB file on the server and display the result on web page,
please if u have any idea how to run matlab from web site tell me
thank u soo much .
This is explained in detail in this guide: http://www.mathworks.com/help/toolbox/compiler/example_guide/brh232k.html
Basically you need to compile your .m file using the Matlab Builder NE and deploy it as a
webservice or normal ASPX file. See here for an overview of the deployment scheme.
This might be a terrible hack but perhaps it helps. I have read that you can make an executable from your MATLAB file. If that is so. The following has worked for me when I need a Web Application to execute an executable and show the results. Be warned, the account your web application runs under will need permissions to run the executable.
Once you have made your MATLAB file into an executable you can create a process, redirect its standard output and place that output in a web element (in this example I used a label).
//Get the path to the executable you wish to run from a setting in web.config
var executablePath = ConfigurationManager.AppSettings["MATLAB_EXECUTABLE_PATH"];
//Create a process to execute the executable. Redirecting the output.
var proc = new Process();
proc.StartInfo = new ProcessStartInfo
{
CreateNoWindow = true,
ErrorDialog = false,
FileName = executablePath,
RedirectStandardError = true,
RedirectStandardOutput = true,
Arguments = args,
UseShellExecute = false //Very important do not leave this out.
};
proc.Start(); //Execute the executable.
lblOutput.Text = proc.StandardOutput.ReadToEnd(); //use the results
lblErrorMessages.Text = proc.StandardError.ReadToEnd(); //Show any error output from the executable.