C# and .NET 3.5 - How to launch a process using different credentials, with a hidden window, and being able to capture standard output and exit code?

孤人 提交于 2019-12-05 22:53:25

I think you want to look at:

ProcessStartInfo

That provides a .NET only way to start a process under a different user with different credentials. No more need for the Win32 API.

--- EDIT ---

Other alternatives might be to use the P/Invoke as specified here (look at the note at the bottom first) or more likely, using P/Invoke with CreateProcessWithLogonW.

Just an unverified thought, but isn't using dynamic Impersonate valid for the entire code block in .net 2+? So Impersonate the required user and then do whatever you need within the impersonated context/scope, like starting a process the normal way? Think we did something like that to integrate mstsc.exe launching using already supplied credentials.

StartInfo has a property called CreateNoWindow that may make Reed's suggestion work for you.

If you want to see those exit codes actually used, have a look at www.robocopyplus.com. it acts like a wrapper around robocopy to add swicthes for email notification and report filtering. This may already do some of the stuff you are trying to achieve.

Matthias Meid

This should work well with the Process class and ProcessStartInfo.

Different credentials:

Redirect standard output:

Exit code:

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!