0

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.

Community
  • 1
  • 1
NandNand
  • 73
  • 1
  • 5
  • 14
  • What for are you creating batch file instead of calling myprogram.exe directly. And what happens when you assign another user to your service instead of Local Service who has enough rights for operation you need? – elevener Aug 14 '11 at 16:18
  • I really don't know how to call myprogram.exe and input command to it directly. could you please give me and example? – NandNand Aug 14 '11 at 16:53
  • I tried System.Diagnostics.Process.Start(@"C:\Program Files\TotalAudioConverter\AudioConverter.exe"); but nothing happen. Y_Y – NandNand Aug 14 '11 at 17:01
  • Your AudioConverter.exe probably needs some arguments? So System.Diagnostics.Process.Start(@"C:\Program Files\TotalAudioConverter\AudioConverter.exe",@"C:\MyFolder\MyInFile C:\MyFolder\MyOutFile"); (or whenever parameters it needs) – elevener Aug 14 '11 at 17:46
  • so it means that i can put all parameters after C:\Program Files\TotalAudioConverter\AudioConverter.exe right? the full parameters are C:\MyFolder\MyInFile C:\MyFolder\MyOutFile c-mp3 so it will look like System.Diagnostics.Process.Start(@"C:\Program Files\TotalAudioConverter\AudioConverter.exe",@"C:\MyFolder\MyInFile C:\MyFolder\MyOutFile -cmp3"); right? really thanks for your help, elevener!!! i will try as soon as i can and will tell you the result :) – NandNand Aug 15 '11 at 01:41
  • yes but you'll still probably have to solve service problem. Have a look at second part of my first question – elevener Aug 15 '11 at 05:08
  • I haven't tried accessing the service using another user. I only tried with Blackberry Simulator, which resides on the same PC. By the way, I think the problem is not how the user using the service. I think it is how to make web service accessing my PC via IIS without permission problems. – NandNand Aug 15 '11 at 06:39

1 Answers1

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.

Hans
  • 12,902
  • 2
  • 57
  • 60
  • Thanks Hansjoerg! I will try and tell you the result! :) – NandNand Aug 16 '11 at 04:57
  • Now, I have wwwroot using DefaultApplicationPool(network service) and MyApp using NewAppPool (local system). I still can't run batch file. I tried put in web.config, but nothing happen. please guide me what mistake i made. – NandNand Aug 16 '11 at 07:51
  • I saw all process in task manager and there was a process of .exe program with user name = system. But I still don't understand why it was never executed. :( – NandNand Aug 16 '11 at 09:07
  • I think your program was never executed because of Windows Vista's user account control (UAC). UAC limits apps to standard user privilegues. – Hans Aug 16 '11 at 19:15
  • Oh no... I am planning to change to another machine(Windows 7). Just hope I would not face this problem again on Windows 7. :( – NandNand Aug 17 '11 at 06:31
  • Under Windows 7 you will face the same problem. UAC applies also to Windows 7. – Hans Aug 17 '11 at 16:43
  • Now i am working on XP, but i am still facing the same problem. what I have done are granting full control permission for ASP.NET user and every one for cmd.exe and every related folder, set local securities and remove asp.net user. Nothing happen again... but i can see the process on task manager that cmd.exe is on process... what should i do??? – NandNand Aug 17 '11 at 17:29
  • is it possible to invoke C# console app from C# web service??? i am thinking of creating c# console app that run the command, and the console app can be invoke from C# web service. is it possible? – NandNand Aug 17 '11 at 17:35