views:

36

answers:

2

Is it possible to move files from a network location that requires credentials to another network location that also requires credentials without mapping any drive. (ie: Without any use of P/Invoke)

Example:

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

This works fine if the source and destination network drives are already mapped but if they are not It doesn't.

A: 

No. You'd need to p/invoke something. This functionality is not provided in the BCL.

Stephen Cleary
+1  A: 

Try something like:

    [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();

This impersonates a domain user, and can then be used to copy files.

taylonr