Batch file not working in server but working local - c#

I created a batch file in my asp.net web site, It is working within the local system without any problem. After I was upload it to my web site's server (shared server), it is not working in the remote environment.
Here is a screenshot of the error I'm receiving
This is the code I'm using to test run the batch on the server.
string path = Server.MapPath("~/SourceCode/Jsil");
Response.Write(path+"<br>");
string batpath=Server.MapPath("~/Dir.bat");
string framework = Server.MapPath("~/SourceCode/v4.0.30319");
string vscompiler = #"\csc.exe /t:exe /r:JSIL.dll;JSIL.Meta.dll Program.cs";
string full = framework + vscompiler;
Response.Write(full);
StreamWriter file = new StreamWriter(batpath);
file.WriteLine("G:");
file.WriteLine("cd " + path);
file.Write(full);
file.Close();
//Excecute bat file
System.Diagnostics.Process compile = new System.Diagnostics.Process();
compile.StartInfo = new ProcessStartInfo(batpath);
compile.Start();
compile.Close();

This might be a simple problem of privileges. The application user might not have execute privilege in the server box. You need to contact your system administrator and ask him to provide execute privilege to the IIS app pool user in the server.

Related

Converting .xls to .xlsx using excelcnv.exe or ofc.exe in C#

I am trying to run a 'EXE' using c# code to convert .xls to .xlsx. It is running successfully and working as expected if I run using a console application or cmd.
But when I am running it in server side (code hosted in IIS i.e code running using w3wp.exe worker process) it unable to convert files to .xlsx
First, I used Excelcnv.exe to convert
if (System.IO.File.Exists(pstrIPDTemplatePath))
{
string pstrOpenXMLIPDTemplatePath = pstrIPDTemplatePath.Replace(".xls", ".xlsx");
string processArguments = "-oice \"" + pstrIPDTemplatePath + "\" \"" + pstrOpenXMLIPDTemplatePath + "\"";
ProcessStartInfo Excelcnv = new ProcessStartInfo();
Excelcnv.FileName = mstrExcelCnvExePath;
Excelcnv.Arguments = processArguments;
Process.Start(Excelcnv);
}
It converted few files successfully but for few files it is creating excelcnv.exe*32 process in task manager, but it is not converting excel file. I even used event handlers for output, but the result is null. Process is going to indefinite state.
But if I am Running it through console application it converted the file successfully it also popped up a dialog after running it to convert multiple files that Excel did not launch correctly and asking whether to start in Safe Mode.
To avoid this safe mode issue, I tried to use ofc.exe
So, I used ofc.exe to convert as below:
Process prc = new Process();
prc.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
prc.StartInfo.FileName = #"OFC.exe";
prc.StartInfo.Arguments = #"ofc.ini";
prc.StartInfo.UseShellExecute = false;
prc.StartInfo.RedirectStandardOutput = true;
prc.Start();
Same case with this code. It is working successfully and converting as expected when running via console application.
It is unable to convert to .xlsx but creating the process in task manager and log file is empty when running using w3wp process hosted in IIS.
Even after giving IIS_IUSRS permission to 'EXE' location, it is not working but its working in console application.
Any ideas why this is happening, and how to get around it?
Try to add your w3wp running user to 'Administrator' group. It will work.
Otherwise use this which is mentioned here

How to fix DirectoryNotFoundException while using Process in Web App

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.

How create Path on a Live server in C# code?

The below code shows how I created a Path/Directory to Local Machine. Now, I want to put my Application to the Live server but my problem it creates a File to a given Path on a Live server, how can I achieve it?
`string appPath = Request.PhysicalApplicationPath;
string IPAddress = HttpContext.Current.Request.UserHostAddress;
Directory.CreateDirectory(appPath + "//PrintFiles/" + IPAddress");
StreamWriter w;
w = File.CreateText(appPath + "//PrintLabels/" + IPAddress + "/printLabels.txt");
w.WriteLine(fileContents.ToString());
w.Flush();
w.Close();`
Same code will work if the server has access to the target location. But the process running the code has to have rights which will allow for directory creation at the target location. If it is a local process to a non local resource, then the process needs either needs to be known to both computers or a system wide process should kick off the code.
For example if this process is in IIS, then the app pool user needs to be set to a process which has access rights to the target location.

Scheduling C# app throughts Webclient exception

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.

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.

Categories

Resources