process - How to run bat file with required permissions in C# -
process - How to run bat file with required permissions in C# -
i have bat file copies files 1 location another.
set src=%1 set dest=%2 xcopy /y/i %src%\*.txt %dest%\temp echo done! i'm trying run file via c# program
var psi = new processstartinfo(filetorun); psi.arguments = args; psi.redirectstandardoutput = true; psi.redirectstandarderror = true; psi.windowstyle = processwindowstyle.hidden; psi.useshellexecute = false; psi.createnowindow = true; process cmdproc = process.start(psi); streamreader output = cmdproc.standardoutput; streamreader errors = cmdproc.standarderror; cmdproc.waitforexit(); bat-file executed, can see 'done!' message in output, files not copied.
the way works is
psi.useshellexecute = true; psi.redirectstandardoutput = false; psi.redirectstandarderror = false; but in case have disable output/error redirection , need them. doesn't work me.
i have tried set administrator's username/password
psi.username = username; psi.password = password; logon succeed, 'the handle invalid' message in standarderror stream.
i guess process i'm trying run doesn't have permissions re-create files , don't know how grant him these permissions.
please, help!
edited
thank replies! have spend several hours trying handle issue , happens have posted question , found solution :)
in order avoid getting 'the handle invalid' message have to
psi.redirectstandardinput = true; but can see cmd.exe window, if username set, bad.
you missing
psi.domain = "domain"; psi.verb ="runas"; //if using local user business relationship need supply machine name domain try simple snippet should work
void main() { string batchfilepathname =@"drive:\folder\filename.bat"; processstartinfo psi = new processstartinfo(batchfilepathname); psi.arguments = "arg1 arg2";//if psi.windowstyle = processwindowstyle.hidden; psi.useshellexecute = false; psi.verb ="runas"; psi.username = "username"; //domain\username psi.domain = "domain"; //domain\username //if using local user business relationship need supply machine name domain psi.windowstyle = processwindowstyle.hidden; psi.useshellexecute = false; psi.verb ="runas"; process ps = new process(psi); process.start(ps); } c# process permissions batch-file
Comments
Post a Comment