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.
Related
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 a requirement to open a windows explorer Path like this "C:\New Folder\" or "http:\gmail.com" using C# code. Actually, once the user types the link\path in the text box and save it, a link should appear and on clicking the link, the required folder path or link should open. My requirement is satisfied with Process.Start(). But, Process.Start is not working in IIS. Can any one suggest any other options. Please.
The code am using is below.
string myPath = #"c:\backoffice";
System.Diagnostics.Process prc = new System.Diagnostics.Process();
prc.StartInfo.FileName = myPath;
prc.Start();
You are misunderstanding the way the web works. If you call Process.Start on the web server, it runs that command on the web server. There is no way that the Process.Start is magically mapped to some action on the client.
Also, you can't just open a specific folder on the client machine from a web site. The security protocols implemented in the browser will prevent that.
I am creating a complex service on Windows 7, which starts off by loading an .exe application. The only way to achieve this was to enable to "Interactive Services Detection" service in services.msc.
As a test application, I added the following code which simply opens the system calculator. It works, however not as intended. I first get the following message when starting the service:
When clicking "View this message", it loads the calculator as it is supposed to, but opens a blue full screen mode, and contains the calculator within it.
My question is basically the following: How can I make the application display automatically, and not have to prompt the user to "view the message", and more importantly, how can I get the application to display normally, rather than in this "interactive services detection" sandbox?
This is the code of my sample service:
public partial class OpenCalculator : ServiceBase
{
public Process process;
public OpenCalculator()
{
this.ServiceName = "Open Calculator";
InitializeComponent();
}
protected override void OnStart(string[] args)
{
start_calc();
}
protected override void OnStop()
{
process.Kill();
}
protected void start_calc()
{
try
{
process = new Process();
process.StartInfo.FileName = #"C:\Windows\system32\calc.exe";
process.StartInfo.CreateNoWindow = true;
process.StartInfo.ErrorDialog = false;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.UseShellExecute = false;
process.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
process.Start();
using (StreamWriter writer = File.AppendText("C:\\Users\\scaruana\\Desktop\\test.txt"))
{
writer.WriteLine(process);
}
}
catch (Exception ex)
{
using (StreamWriter writer = File.AppendText("C:\\Users\\scaruana\\Desktop\\test.txt"))
{
writer.WriteLine(ex.Message);
}
}
}
}
Services run in session 0, whose desktop you cannot see. Interactive desktops are hosted in other sessions. So, for instance, the first interactive logon runs in session 1. This means that services cannot directly show UI on an interactive desktop.
In older versions of Windows (XP and earlier), the first interactive logon shared session 0 with services. And so when you enabled the allow service to interact with desktop option in the service control manager, the service could show UI on an interactive desktop. However, all that ended with Vista because it was a security risk.
So, you need to start a new process and specifically force it onto the desktop of an logged in interactive user. That is not easy to do. This MSDN blog article covers your options: Launching an interactive process from Windows Service in Windows Vista and later.
Now, that's how you go about starting a new process from a service, and putting the new process onto an interactive desktop. But that is almost certainly the wrong way to solve your problem. As you can see by reading the linked article, doing it that way is complex. The standard solution to your problem is to run an interactive process on the logged in user's desktop. This remains hidden until your service needs to show UI. The service then communicates with the desktop app using your preferred IPC mechanism, and then the desktop app shows the UI.
This question has been the cause of great frustration, and I have finally solved my problem. Yes, I have managed to make a service load a GUI application, even though everyone says that it is impossible. There is a warning though - the "fix" can be considered as exploiting a loophole in Windows, as the code which I used basically passes the Vista and Windows 7 UAC. Essentially, the application is always executed with full rights and bypasses the UAC.
If anyone has this same problem, what you need to do is iterate through a list of logged in users on the PC, and choose to open the UI application in this session (which has the user's desktop), rather than in session 0 where the service is supposed to be running (and which does not have a desktop).
For some people, this might not be a solution as it is not secure. But in my case, security is not a main concern, and I just needed it to work (had to be a service by force).
Hope this helps anyone who has the same problem that I had.
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.
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.