using StreamWriter on server in C#

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-01 10:49:13

问题


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 and 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?


回答1:


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);
}


来源:https://stackoverflow.com/questions/6829432/using-streamwriter-on-server-in-c-sharp

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