我正在使用 C#...
我需要能够将一组文件复制到大约 500 台不同的计算机上。我已成功地使用 LogonUser() 方法来模拟具有复制文件所需权限的域帐户。文件的目标路径类似于:
\\远程计算机\C$\SomeFolder
我的问题是......有没有办法做到这一点,而不必使用全能的域帐户(这些计算机将来可能不会加入域)?我每台计算机都有本地管理员帐户...是否有一种简单的方法可以使用本地管理员帐户而不是域帐户将文件复制到计算机?
如果我错了请纠正我,但你可以使用登录用户 http://msdn.microsoft.com/en-us/library/aa378184.aspx模拟本地组也不仅仅是域帐户。
来自网络: http://social.msdn.microsoft.com/forums/en-US/vblanguage/thread/b9906632-5cad-4972-9103-3e864a71a0c2/
Imports System
Imports System.Runtime.InteropServices
Imports System.Security.Principal
Imports System.Security.Permissions
Public Class Form1
<DllImport("advapi32.DLL", SetLastError:=True)> _
Public Shared Function LogonUser(ByVal lpszUsername As String, ByVal lpszDomain As String, _
ByVal lpszPassword As String, ByVal dwLogonType As Integer, ByVal dwLogonProvider As Integer, _
ByRef phToken As IntPtr) As Integer
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim admin_token As IntPtr
Dim wid_current As WindowsIdentity = WindowsIdentity.GetCurrent()
Dim wid_admin As WindowsIdentity = Nothing
Dim wic As WindowsImpersonationContext = Nothing
Try
MessageBox.Show("Copying file...")
If LogonUser("Local Admin name", "Local computer name", "pwd", 9, 0, admin_token) <> 0 Then
wid_admin = New WindowsIdentity(admin_token)
wic = wid_admin.Impersonate()
System.IO.File.Copy("C:\right.bmp", "\\157.60.113.28\testnew\right.bmp", True)
MessageBox.Show("Copy succeeded")
Else
MessageBox.Show("Copy Failed")
End If
Catch se As System.Exception
Dim ret As Integer = Marshal.GetLastWin32Error()
MessageBox.Show(ret.ToString(), "Error code: " + ret.ToString())
MessageBox.Show(se.Message)
Finally
If wic IsNot Nothing Then
wic.Undo()
End If
End Try
End Sub
End Class
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)