Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


How to execute a DOS command from an ASP.NET application deployed on the remote network server?

Posted on 2004-11-01
Medium Priority
Last Modified: 2012-05-05
I have to map a samba share on a remote server from an ASP.NET application using the NET USE command.  I was able to do it if the ASP.NET application was created, compiled, and located on my local server.  But after I move it to the network server, the NET USE returns "Access Denied".  To simplify the analysis, I am trying to execute a simple copy command instead, but still, it works on the local server, but does not work on the remote one.

I probably either have permissions problem or miss something doing the deployment.
I use the following C# code:

// some code . . . . . . . .

System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo.UseShellExecute = false;

/* the CopyFile.bat contains just one statement:
copy c:\devel\uploads\test1.txt C:\devel\test\test1.txt

//// other code…
// . . . . . . . . . . . . . . . . . . . . . . . .
catch (Exception exc)
//   The code to display the exc.Message
//. . . . . . . . . . . .

Again, when I try this code on a local server, a file is being copied.
After I move the application to the remote server, exception “Access Denied” is being thrown.

I tried to add the ASPNET account on the remote server to the Administrators group.  It did not work.
On the local server it works OK without adding the ASPNET account to the Administrators group.

To deploy, I just copied the application’s files to the appropriate remote server’s IIS directory.
The page is displayed OK, and only the attempt to execute a command fails.

If I execute the CopyFile.bat from the Windows START/Run box, it works OK on both local and network server, therefore the copy command is OK.

Any suggestions will be much appreciated.
Question by:pavelmed
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 5
LVL 17

Expert Comment

ID: 12467662
Process MyProcess = new Process();
MyProcess.StartInfo.FileName = "cmd.exe";
MyProcess.StartInfo.UseShellExecute = false;
MyProcess.StartInfo.CreateNoWindow = true;                
MyProcess.StartInfo.RedirectStandardInput = true;

MyProcess.StartInfo.RedirectStandardOutput = true;

StreamWriter MyStreamWriter = MyProcess.StandardInput;
StreamReader MyStreamReader = MyProcess.StandardOutput;

MyStreamWriter.AutoFlush = true;
MyStreamWriter.Write(@"c:\test.bat" + System.Environment.NewLine);
MyStreamWriter.Write("exit" + System.Environment.NewLine);
string s = MyStreamReader.ReadToEnd();
textBox1.Text = s;
LVL 17

Expert Comment

ID: 12467681
/ Get the full file path
string strFilePath = “c:\\temp\\test.bat”;

// Create the ProcessInfo object
System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo("cmd.exe");
psi.UseShellExecute = false;
psi.RedirectStandardOutput = true;
psi.RedirectStandardInput = true;
psi.RedirectStandardError = true;
psi.WorkingDirectory = “c:\\temp\\“;

// Start the process
System.Diagnostics.Process proc = System.Diagnostics.Process.Start(psi);

// Open the batch file for reading
System.IO.StreamReader strm = System.IO.File.OpenText(strFilePath);

// Attach the output for reading
System.IO.StreamReader sOut = proc.StandardOutput;

// Attach the in for writing
System.IO.StreamWriter sIn = proc.StandardInput;

// Write each line of the batch file to standard input
while(strm.Peek() != -1)


// Exit CMD.EXE
string stEchoFmt = "# {0} run successfully. Exiting";

sIn.WriteLine(String.Format(stEchoFmt, strFilePath));

// Close the process

// Read the sOut to a string.
string results = sOut.ReadToEnd().Trim();

// Close the io Streams;

// Write out the results.
string fmtStdOut = "<font face=courier size=0>{0}</font>";
this.Response.Write(String.Format(fmtStdOut,results.Replace(System.Environment.NewLine, "<br>")));

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

LVL 17

Expert Comment

ID: 12467807

Author Comment

ID: 12473252
I tested the code suggested by AerosSaga.  The result is the same as with my code: The porgram executes OK while running on my local server but throws "Access is denied" exception while starting the process on the network server
(during the execution of the following line:
System.Diagnostics.Process proc = System.Diagnostics.Process.Start(psi);

So there is some difference between running on a local and remote server, and this difference prevents the program from starting a process on a remote server.

Any suggestions will be much appreciated.

LVL 17

Expert Comment

ID: 12473274
Well then it has to be a permissions issue.

Author Comment

ID: 12473502
Yes, the chances are this is a permission issue.  But how to deal with it?  The program starts the process OK on a local server.  What is the difference for the remote server?

LVL 17

Expert Comment

ID: 12473532
well it either has to be the account the asp.net application is running under or the remote machine account.  I'd start with those two.
LVL 17

Expert Comment

ID: 12473663
I suspect the account your asp.net application is running under does not have sufficient privaleges on the remote machine you are trying to execute the .bat on.

Author Comment

ID: 12475669
I assume that the application runs under ASPNET account on both local and remote servers.
On both machines this account is a member of Users group which has the same permissions setting.
The security setting for the IIS directory is also the same on both machines.

What are other privilegis that I could miss?

LVL 17

Expert Comment

ID: 12477586
It is still acting under the first asp.net account when it access the .bat file, so that account will need to be a domain account.  

Author Comment

ID: 12533804
I was unable to get the ASP.NET application to run the batch file on the network server while it was working on my local server.
The difference is that the network server is a Windows 2003 Server which has a tighter security.

Does anyone have a suggestion how to run a batch file from an ASP.NET application on a Windows 2003 Server?
Or any other way to accomplish the goal: upload a file to a samba share directory?

Thanks a lot!!
LVL 17

Accepted Solution

AerosSaga earned 1500 total points
ID: 12533832
are you doing these things?

1. I created a domain user to give write permissions to each of the share drives on each server.
2. I used impersonation (http://support.microsoft.com/default.aspx?scid=kb;en-us;306158) to change the user while creating the folder and executing the upload to the user I created.
<identity impersonate = "true"/>
Doing this did 3 things:
1. allowed me to identify the user upon them accessing my page.
2. impersonated a user with write permissions to each server while executing the upload.
3. returned the authentication back to the users domain credentials after the upload.

Author Comment

ID: 12534918
I tried to test the impersonation setting <identity impersonate = "true"/> on my local server (Windows XP) where the application actually works without the impersonation (it executes the batch file).  However, after I added the impersonation setting to the web.config file, it stopped copying the file without any warning or exceptions. (The bach file for the test contained just one statement:  copy c:\devel\uploads\test1.txt C:\devel\uploads\test\test1.txt ).  Correspondingly, it did not work on the Windows 2003 Server (although it also stopped throwing exception "Access Denied").

On my local server I tried to use the impersonation setting with credentals
<identity impersonate = "true" userName="myname" password="mypass" /> where myname and mypass belong to the administation group, however, the file was not copied either.

So the impersonation dod not work for me even on a local server.  I could have done something wrong with the impersonation setting, but I don't see what.

I have also tried to create a domain user.  Not sure I did it correct.  I added my user name with a domain_name\ prefix to the Power Users group using the Computer Management tool.  How else can I create the the Domain user?

Finally, I think I need to run a batch file on the server because I need to copy an uploaded file to the samba share directory.
If I needed just to copy a file, I would just use the PostedFile.SaveAs("... ") - it worked OK for the server's folder.
However, I need to copy a file to a samba share, and for a real application I use the batch file to create a map to that share and then to copy a file to that map.  Again, it does not work on a Windows 2003 server because I can't execute the batch file.
Any other suggestions how to do it???

Thanks a lot

Featured Post


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Screencast - Getting to Know the Pipeline
Suggested Courses

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question