使用凭据的网络IO

时间:2010-05-26 13:54:19

标签: c# file-io networking filesystems

是否可以将文件从需要凭据的网络位置移动到另一个网络位置,该位置也需要凭据而不映射任何驱动器。 (即:不使用P / Invoke)

示例:

FileInfo fi = new FileInfo(@"\\SomeComputer\SomeDrive\SomeFolder\someFile.txt");
fi.MoveTo(@"\\AnotherComputer\AnotherDrive\AnotherFolder\AnotherFile.txt");

如果源网络驱动器和目标网络驱动器已经映射但是如果它们不是映射,则此方法可以正常工作。

2 个答案:

答案 0 :(得分:3)

尝试类似:

    [DllImport("advapi32.dll")]
    private static extern int LogonUser(string lpszUsername, string lpszDomain, string lpszPassword, int dwLogonType, int dwLogonProvider, ref IntPtr phToken);

    /// <summary>
    /// Used for logging on the domain
    /// </summary>
    public enum LogonProvider
    {
        /// <summary>
        /// 
        /// </summary>
        LOGON32_PROVIDER_DEFAULT = 0,
        /// <summary>
        /// 
        /// </summary>
        LOGON32_PROVIDER_WINNT35 = 1,
        /// <summary>
        /// 
        /// </summary>
        LOGON32_PROVIDER_WINNT40 = 2,
        /// <summary>
        /// 
        /// </summary>
        LOGON32_PROVIDER_WINNT50 = 3
    };

    /// <summary>
    /// Used for logging on across the domain
    /// </summary>
    public enum LogonType
    {
        /// <summary>
        /// 
        /// </summary>
        LOGON32_LOGON_INTERACTIVE = 2,
        /// <summary>
        /// 
        /// </summary>
        LOGON32_LOGON_NETWORK = 3,
        /// <summary>
        /// 
        /// </summary>
        LOGON32_LOGON_BATCH = 4,
        /// <summary>
        /// 
        /// </summary>
        LOGON32_LOGON_SERVICE = 5,
        /// <summary>
        /// 
        /// </summary>
        LOGON32_LOGON_UNLOCK = 6,
        /// <summary>
        /// 
        /// </summary>
        LOGON32_LOGON_NETWORK_CLEARTEXT = 8,
        /// <summary>
        /// 
        /// </summary>
        LOGON32_LOGON_NEW_CREDENTIALS = 9
    }
 IntPtr token = new IntPtr();
 LogonUser(<username>, <domain>, <password>, (int)LogonType.LOGON32_LOGON_NEW_CREDENTIALS, (int)LogonProvider.LOGON32_PROVIDER_WINNT50, ref token);
 WindowsIdentity w = new WindowsIdentity(token);
 w.Impersonate();

此模拟域用户,然后可用于复制文件。

答案 1 :(得分:0)

没有。你需要p / invoke 某些东西。 BCL中未提供此功能。