Due to my problem that I am unable to run dos command via my web service.
C# web service running batch file or dos command?
Since I cannot make my web service run dos command directly, I am now thinking about creating C# console app that will run my dos command, then the console app will be invoked by web service.
Is it possible to do so?
The easiest method to call a .Net console application from another .Net application is to link in the assembly, and invoke the static
Main
method directly. But if there is any reason you can't execute commands (permissions), then you'll have the same problems with this method.If permissions are the problem, then you could:
Also, as John Kalberer mentioned, it could be related to the inability of these services to interact with the desktop. If this is the problem, then see this question.
If it's possible from within a web service, you'll need to do one of two things:
Assuming that one of the above works and you're able to execute the console app, there are also a few other things you'll need to look into:
I had to do this once before, and standard impersonation didn't work for me. I had to handle impersonation a little differently. I don't know if you'll run into the same obstacles that I did, but here is a class that I created for impersonating programmatically through the Windows API:
EDIT
Changed to an instance class implementing IDisposable - thanks @John Saunders
Added an overloaded constructor for easier implementation with using statement, and added boolean property Impersonating to check whether the instance is currently impersonating. There are also BeginImpersonationContext and EndImpersonationContext methods for alternative use.
EDIT
Lastly, here is an example of how to implement the class: