I have prblem with my C# application. I need to move file to my server, but my server is secured with user name and password. Way is like this:
\\Server\Folder
And code is this:
...
File.Move(args[0], "\开发者_高级运维\\\Server\\Folder");
...
How can I move file there? Please help me.
Use impersonation:
AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);
WindowsIdentity idnt = new WindowsIdentity(username, password);
WindowsImpersonationContext context = idnt.Impersonate();
File.Move(args[0], "\\\\Server\\Folder");
context.Undo();
You could P\Invoke the Windows API function WNetAddConnection2
to connect to the share before copying.
http://msdn.microsoft.com/en-us/library/aa385413(v=vs.85).aspx
Here is the PInvoke page: 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 );
精彩评论