Calling an exe file from web application - c#

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.

Related

execute an EXE on the server

I have an exe which I call from the command line. Is it possible to execute that file on the server? On the computer if the file is located in the folder abc, I go to folder abc and than I execute the batch. Hw do I do this in C#
Code example below, make sure you have your permissions setup correctly:
System.Diagnostics.Process yourProcess = new System.Diagnostics.Process();
// Set the directory
yourProcess.StartInfo.WorkingDirectory = Request.MapPath("~/"); //or wherever your file is
// Set the filename
yourProcess.StartInfo.FileName = Request.MapPath("bla.exe");
// Start the process
yourProcess.Start();
ASP Net - Run Application (EXE) from ASP.Net C#
In server side code certainly, Process.Start(MyExeFile) will do that but, as long as the user account you are running your stuff on can execute it.

launch console application from asp.net page on shared hosting

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.

How to start the bat-file at Asp.net web server side?

I need to call a console application to load data into another desktop application on the remote server that located within the corporate domain.
Users will enter the web page and upload data to asp.net web server, which after transformation should call that console application. Users are located remotely and do not have any other access except the web server.
I decided to lower the security web application context and let the asp.net working process to start the console application on the current IIS 6.0 web server
What I have done:
I changed the security account for the application pool for Local System;
I added ASPNET Account and IIS_WPG IIS Process Account to Administrators group;
I added “Allow service to interact with desctop” for “IIS Admin Service” and “World Wide Web Publishing Service” processes and restarted the machine;
I tried to start BAT-file at server side through the test page code-behind, but failed:
protected void btnStart_Click(object sender, EventArgs e)
{
Process process = new Process();
process.StartInfo.FileName = #”C:\run.bat”;
process.StartInfo.UseShellExecute = false;
process.Start();
process.WaitForExit();
}
The error was access denied.
Please help me to find any workable idea how to start the bat-file at web server side.
Thanks
Try setting UseShellExecute to true instead of false. After all, batch files run in a shell - so you need a shell to execute it. (Another option is to run cmd.exe and pass the name of the batch file in as an argument, e.g. "cmd.exe /k c:\run.bat")
You might also want to try creating a simple .NET app which just (say) creates a file with a timestamp in. That way you can test the "can I start another process" bit separately from the "can I get the batch file to work" bit.
Put that particular batch file in your application itself.
string str_Path = Server.MapPath(".") + "\\run.bat";
ProcessStartInfo processInfo = new ProcessStartInfo(str_Path);
processInfo.UseShellExecute = false;
Process batchProcess = new Process();
batchProcess.StartInfo = processInfo;
batchProcess.Start();
Take a look at this example: Run Interactive Command Shell or Batch Files From ASP.NET
It uses little different approach. They suggest running cmd.exe and executing command line by line.

Run matlab and excute file.m from website

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.

Is it possible to launch Download Manager (a process) in .NET

As the title says, it is possible to launch Download Manager (such as Orbit) passing the URL of the file to download?
Edit:
How can I check how those programs are expecting the url???
What parameters are needed to be passed ??
My problem is , I do NOT know how those programes are expecting their inputs.
Before that, I also would like to check if one of download managers has installed in the machine.
If you want to start an application from C# you can create a new process of the .exe file of the application and if that application supports arguments you can add them also. So you will need to find out if Orbit accepts arguments. The example below shows how to start a new process with arguments. Please note that the application will start out of the process of your own application.
string args = " \"" + URL + "\"";
ProcessStartInfo psi = new ProcessStartInfo("path to Orbit", args);
Process p = Process.Start(psi);
string path = #"C:\Program Files\Orbit\Orbit.exe";
string arg = #"http://www.example.com/file.ext";
System.Diagnostics.Process.Start(path, arg);
There is no way to find an abstract downloader on a computer and launch it directly to download a file.
Only one idea I have - launch a browser and hope it will download file by itself or launch a registered downloader:
Process.Start("http://example.com/file.ext");
If the download manager accepts url arguments then you can start a process and pass the desired URL to it
Process.Start("Orbit.exe -http://www.something.com/file.ext");
So this approach highly depends on the download manager.
I guess it is kind of depends in the download manager.
If it has support for command line arguments with the file location URL then you can use the System.Diagnostics.Process class.

Categories

Resources