C# 使用凭据的网络IO

C# 使用凭据的网络IO,c#,file-io,networking,filesystems,C#,File Io,Networking,Filesystems,是否可以将文件从需要凭据的网络位置移动到另一个也需要凭据的网络位置,而无需映射任何驱动器。(即:不使用P/Invoke) 例如: FileInfo fi = new FileInfo(@"\\SomeComputer\SomeDrive\SomeFolder\someFile.txt"); fi.MoveTo(@"\\AnotherComputer\AnotherDrive\AnotherFolder\AnotherFile.txt"); 如果源和目标网络驱动器已映射,则此操作正常,但如果它们

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

例如:

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

如果源和目标网络驱动器已映射,则此操作正常,但如果它们未映射,则不会映射。

否。您需要p/调用某些内容。BCL中未提供此功能。

请尝试以下操作:

    [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();
[DllImport(“advapi32.dll”)]
私有静态外部int LogonUser(字符串lpszUsername、字符串lpszDomain、字符串lpszPassword、int dwLogonType、int dwLogonProvider、ref IntPtr phToken);
/// 
///用于登录域
/// 
公共枚举登录提供程序
{
/// 
/// 
/// 
LOGON32\u提供程序\u默认值=0,
/// 
/// 
/// 
LOGON32\u提供者\u WINNT35=1,
/// 
/// 
/// 
LOGON32\u提供程序\u WINNT40=2,
/// 
/// 
/// 
LOGON32\u提供程序\u WINNT50=3
};
/// 
///用于跨域登录
/// 
公共枚举登录类型
{
/// 
/// 
/// 
LOGON32\u LOGON\u INTERACTIVE=2,
/// 
/// 
/// 
LOGON32\u LOGON\u网络=3,
/// 
/// 
/// 
LOGON32\u LOGON\u批=4,
/// 
/// 
/// 
LOGON32\u LOGON\u服务=5,
/// 
/// 
/// 
LOGON32\u LOGON\u UNLOCK=6,
/// 
/// 
/// 
LOGON32\u LOGON\u NETWORK\u CLEARTEXT=8,
/// 
/// 
/// 
LOGON32\u LOGON\u NEW\u凭据=9
}
IntPtr令牌=新的IntPtr();
LogonUser(,,(int)LogonType.LOGON32\u LOGON\u NEW\u凭据,(int)LogonProvider.LOGON32\u PROVIDER\u WINNT50,ref-token);
WindowsIdentity w=新的WindowsIdentity(令牌);
w、 冒充();
这将模拟域用户,然后可用于复制文件