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.
Related
On application start, I want to store certain application configuration keys (ex: directory path, database connection string) as an environment variable by running a batch script. I have tried this by writing Powershell script as guided in this article here, which works absolutely fine. I tried the same using the batch file like below but it doesn't seem to be working.
Below is sample of .bat file which I am trying to load/configure at the application start.
EDIT: Correcting .bat the script as suggested by Compo in the comment section below.
#echo off
SET "LOG=api.log"
SET "DATABASE_URL=host=xyz.rs.aws.com dbname=somename user=someuser password=somepassword sslmode=enable"
SET "LISTEN_PROTOCOL=http"
#echo on
Below is sample code used to run the sample.bat file and fetch the value,
string batFilePath = string.Format(#"D:\sample.bat");
Process proc = new Process();
proc.StartInfo.WorkingDirectory = batFilePath ;
proc.StartInfo.CreateNoWindow = true;
proc.Start()
proc.WaitForExit();
// fetch the value set by batch file
var data = Environment.GetEnvironmentVariable("LOG"); // returns null here
However, I also wanted to know, Is it possible to create and store environment variables at the application process level? i.e. I should be able to use these environment variables only within the application level, not at the system/user level.
I am using LibreOffice as command line for conversion of docx to pdf. I am using below code snippet.
using (Process pdfprocess = new Process())
{
pdfprocess.StartInfo.UseShellExecute = true;
pdfprocess.StartInfo.LoadUserProfile = true;
pdfprocess.StartInfo.FileName = "soffice.exe";
pdfprocess.StartInfo.Arguments = "-norestore -nofirststartwizard -headless -convert-to pdf C:\\test.docx";
pdfprocess.StartInfo.WorkingDirectory = #"C:\Program Files\LibreOffice\program\";
pdfprocess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
pdfprocess.Start();
if (!pdfprocess.WaitForExit(1000 * 60 * 1)) {
pdfprocess.Kill();
}
pdfprocess.Close();
}
Everything works fine under IISExpress or Console application. When I try to run under IIS server, it doesn't work.
I am running under DefaultAppPool and I have given permission to DefaultAppPool to access LibreOffice directory but I am not able to get result.
I don't want to change Identity to LocalSystem as security concerns.
How can I able to run soffice.exe using Process.Start() under default ApplicationPoolIndentity?
I had the same problem and I just found solution that worked for me. When I was executing conversion under CMD console everything was working fine. But soffice.exe executed under iis app didn't work.
Although, application pool has it's own user profile directory it looks like libreoffice cannot create its files there. What I did is I created temp folder under iis www directory and gave it apppool permisions. Then I passed this location with other parameters like that: "-env:UserInstallation=file:///C:/www/temp/libreoffice"
Had same issue. The problem was that process started, but didn't exit and give no results, any StandardError output. It was definitely a problem of permissions. So I've started procmon and it showed me, it needs access to this path:
C:\Windows\SysWOW64\config\systemprofile\AppData\Roaming\LibreOffice
Looks like similar problem here:
Why does systemprofile need Desktop folder to open excel file
So not sure what to do next from security point of view, but solution is to give access to that folder for ApplicationPoolUser.
UPDATED:
Looks like it works not for all documents, and even after I eliminated all ACCESS DENIED issues from Procmon for other docx file, "soffice" still stuck, and I cannot kill process from Task Manager, it gives "Access Denied" dialog. Looks like it depends on some fonts, it opens something, and because it hold it, process cannot be closed, and no more files proceed.
I have developed a c# application to download a zip file from a site, when manually run the app, it does its job normally and exits, But when i schedule it to run automatically using task scheduler in Windows, it throws web client exception error. Please help me regarding this
The part of code for downloading the file is
WebClient wc = new WebClient();
wc.DownloadFile(<site>, "feed.zip");
System.Diagnostics.ProcessStartInfo pinfo = new System.Diagnostics.ProcessStartInfo("extract.bat");
System.Diagnostics.Process process = new Process();
pinfo.CreateNoWindow = true;
pinfo.WorkingDirectory = Directory.GetCurrentDirectory();
process.StartInfo = pinfo;
process.Start();
process.WaitForExit();
The batch file extracts the zip file.
I see these possible problems:
If you run your application Directory.GetCurrentDirectory() could result in a different path. Directory.GetCurrentDirectory() will be set to your bin-folder, but in your production environment, it can point every, depending how your process is executed (for example: if you create a shortcut on your desktop, you can also modify the Start in-folder.
If your current directory is configured wrong, the application would also not be able to find extract.bat.
While you are debugging you have some rights on your outputfolder. But when your application is executed as a batch process, it runs under a different account. Does this account have to rights to write to your output folder?
The website your are accessing might require a valid account. You possibly have a valid account. But the account your application is running from in production, might not be valid account for that website.
You are possibly behind a proxy. Perhaps you need to configure some extra settings for getting past that proxy. In your account, these settings are configured inside Internet Explorer. Perhaps the production account does not have these settings configured.
i have build script in remote machine.but i want to start the build from my local machine.so for this i need to update the input.properties file in remote machine and then run the batch file to start the build process. For this i have created one web page
so how can i modify the remote input.properties file and run the batch file in C#.
please give me some suggestion for this.
thanks in advance...
You need to edit the properties file remotely, using a Stream Reader / Stream Writer. There are many ways to do that. You should be able to solve that yourself.
Once you are happy that the properties file is updated correctly you will need to use PSEXEC service to launch the Batch file locally on the User's machine. So in this case, the batch file needs to be copied over to the remote machine.
What I normally do is write the Batch file your intending on using to the remote machine on the fly, and as soon as I can see that the associated process has finished, I remove the batch file from the local machine.
PSEXEC will be ran at your end, you use it to connect up to the end users machine and fire the batch file.
You can create a process like below :-
Process p = new Process();
p.StartInfo.CreateNoWindow = true;
p.StartInfo.UseShellExecute = false;
p.StartInfo.FileName = "C:\\psexec.exe";
p.StartInfo.Arguments = "\\\\" + computerName + " C:\\YourBatFile.bat";
p.Start();
p.WaitForExit();
This will open a process on the local machine, that will launch your batch file.
I would recommend learning about the PSEXEC Service, its how I've always launched remote processed.
Hope this helps.
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.