C# 部份
private IntPtr token;
private IntPtr tokenDuplicate;
Boolean loginSuccess = true;
if (!NativeMethods.LogonUser ("username", "ip", "password", NativeMethods.LogonType.Batch, NativeMethods.LogonProvider.Default, out token)) {
if (!NativeMethods.LogonUser ("username", "ip", "password", NativeMethods.LogonType.NewCredentials, NativeMethods.LogonProvider.Default, out token)) {
//Incorrect username or password
loginSuccess = false;
}
}
if (!NativeMethods.DuplicateToken (token, NativeMethods.SecurityImpersonationLevel.Impersonation, out tokenDuplicate)) {
throw new Win32Exception ();
}
using (WindowsImpersonationContext impersonationContext =
new WindowsIdentity (tokenDuplicate).Impersonate ()) {
if (loginSuccess) {
//進行複製檔案動作
File.Copy (@"\\" + "ip" + "fileName", @"C:\" + "fileName", true);
}
}
注意事項
其他電腦需將 Folder 存取權限設為分享