C# – Access another PC’s folder

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 存取權限設為分享

開始在上面輸入您的搜索詞,然後按回車進行搜索。按ESC取消。

返回頂部