C# web service running batch file or dos command?

2020-05-06 14:24发布

问题:

From my previous question, Executing batch file in C# web service

I have checked the permission of all related stuff and found that:

batch file: permission = full control for all user

IIS user = myUser

..win32\cmd.exe: permission for system, admin, myUser = read&execute, read permission for "TrustedInstaller"(what is it?) = full control

Visual Studio is running under administrator mode.

I did some research on the Internet and it seems like I need to do something with 'Local Policies' on my machine. Unfortunately, I am using Windows Vista Home Premium, which seems like I cannot set local policies... I also try changing permission of cmd.exe to 'Full control', but it just doesn't allow me to do so.

It seems like I am facing an 'unsolvable' problem, so I will explain what I am doing so that someone may give me an idea what to do.

  • I have developed Blackberry application using blackberry simulator. It is on the same PC as my web service.

  • I developed web service in C#, published in my localhost.

  • Blackberry app upload a file to web service. Web service stores the file on my PC. (I have no problem with this part.)

  • There is one program on my PC, I will call it myprogram.exe. The program only runs on dos. The program will convert file from one file type to another file type using the command myprogram.exe path\oldfile.x path\newfile.y newfile.y will be created and stored on my PC automatically.

  • I need to convert the file that was sent from BB and is now storing on my local PC, so I wrote 1 batch file containing dos command. Web service should execute the batch file, but it wouldn't.

If i stop trying to work with batch file, is there any way i can to to execute those command? (or any way that I can run that external program) Please give me an idea. I have searched on the Internet and still doesn't get any idea. I don't know what to do now. T_T

PS. Forgive me if my questions are stupid. I just want to try every possible way to make this part of project success.

回答1:

All I can think of is:

  1. Your ASP.NET website runs under the DefaultAppPool. The DefaultAppPool has only limited rights.

    In order to execute a batch file from within your service create a new application pool.

    a) Open Internet Information Services Manager (IIS 7). You will find the Internet Information Services Manager in your Windows Vista start menu.

    b) In the tree on the left side right click Application Pools and add a new application pool.

    c) Select your newly created application pool and click Advanced Settings.

    d) Under identity choose LocalSystem.

    e) In the tree select your web site (e.g. Default Web Site). Select basic settings and choose your created application pool.

    f) Restart your web site (right click your web site, under Manage your web site select restart).

    You should be able to execute your batch file. Here is an example:

    Process oProcess = new Process();

    oProcess.StartInfo.UseShellExecute = false;

    oProcess.StartInfo.WorkingDirectory = @"c:\temp\test";

    oProcess.StartInfo.FileName = @"c:\windows\system32\cmd.exe";

    oProcess.StartInfo.CreateNoWindow = true;

    oProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;

    oProcess.StartInfo.Arguments = @"/C c:\temp\test\mybatch.bat";

    oProcess.Start();

    However, I do not recommend this approach.

  2. Rethink your design. I would recommend using a WCF-Service hosted in a Windows Service to execute your batch file. Then from your web service call the WCF-Service to execute your batch file like so (pseudo code):

    var wcfClient = new ConverterServiceClient();

    wcfClient.Convert(@"c:\temp\oldfile.x", "c:\temp\newfile.y");

    // Here your service has converted the file using your application.

    If you are tied to the .NET Framework 2.0 you could host a .NET Remoting service in your Windows Service application.

Hope, this helps.