我的C#应用程序问题。我需要将文件移动到我的服务器,但我的服务器使用用户名和密码进行保护。方式是这样的:
\\Server\Folder
代码就是这样:
...
File.Move(args[0], "\\\\Server\\Folder");
...
如何在那里移动文件?请帮帮我。
答案 0 :(得分:3)
使用模拟:
AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);
WindowsIdentity idnt = new WindowsIdentity(username, password);
WindowsImpersonationContext context = idnt.Impersonate();
File.Move(args[0], "\\\\Server\\Folder");
context.Undo();
答案 1 :(得分:0)
您可以在复制前调用Windows API函数WNetAddConnection2
连接到共享。
http://msdn.microsoft.com/en-us/library/aa385413(v=vs.85).aspx
以下是PInvoke页面:http://pinvoke.net/default.aspx/mpr/WNetAddConnection2.html
[DllImport(“Mpr.dll”,EntryPoint =“WNetAddConnection2”,CallingConvention = CallingConvention.Winapi)] private static extern ErrorCodes WNetAddConnection2(NETRESOURCE lpNetResource,ref string lpPassword,ref string lpUsername,System.UInt32 dwFlags);