开发者

using StreamWriter on server in C#

开发者 https://www.devze.com 2023-03-23 03:25 出处:网络
I try to write to a remote machine with the following code: StreamWriter(@\"\\\\\" + remoteMachine + \"\\\\admin$\\\\\" + fileName);

I try to write to a remote machine with the following code:

StreamWriter(@"\\" + remoteMachine + "\\admin$\\" + fileName);

i get the following error Logon failure: unknown user name or bad password.

i got the user name, domain a开发者_StackOverflow社区nd password. how can i write with credentials?

if i already been to that computer in this session there is no problem, but if it's a new session the computer doesn't recognize the location

how can i write with credentials?


You'll need to impersonate the identity before using the StreamWriter. WindowsIdentity.Impersonate Method article has a great example of doing so.

Inside this code you would use the StreamWriter:

using (WindowsImpersonationContext impersonatedUser = newId.Impersonate()) {
    StreamWriter(@"\\" + remoteMachine + "\\admin$\\" + fileName);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消