首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用远程管理凭据将文件复制到远程计算机

使用远程管理凭据将文件复制到远程计算机
EN

Stack Overflow用户
提问于 2009-04-19 20:25:58
回答 2查看 52.3K关注 0票数 12

我在用C#..。

我需要的能力,复制一套文件到大约500台独特的计算机。我已经成功地使用LogonUser()方法来模拟具有复制文件所需权限的域帐户。文件的目标路径如下:

\RemoteComputer\C$\SomeFolder

我的问题is...is有一种方法可以做到这一点,而不必使用一个全能的域帐户(这些计算机可能不会在未来加入域)?我有每个computer...is的本地管理员帐户,有一种简单的方法可以使用它的本地管理员帐户而不是域帐户将文件复制到计算机上?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2009-04-19 21:00:48

如果我错了,请纠正我,但是您可以使用LogonUser来模拟本地组,而且不仅仅是域帐户。

从网上:

代码语言:javascript
复制
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 
票数 8
EN

Stack Overflow用户

发布于 2009-04-19 20:36:42

WNetAddConnection2会做到这一点的。只需为本地设备名称使用空字符串,以避免映射驱动器。您还希望在完成之后确保和关闭连接。我将它包装成一个实现IDisposable的IDisposable类。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/766033

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档