プログラムからファイル共有をしているPCに資格情報を送ってファイルをコピーするサンプルを掲載します。すこし応用するとネットワークドライブをプログラム上から割り当てることができます。

確認環境

  • コピーするクライアントXP, ファイル共有をするサーバWindows 2003
  • 作成環境: Visual Studio 2005 .NET 2.0

プログラムは次の通りです。Win32APIのWNetAddConnection2を使用していますので、必要に応じてMSDNを参照して下さい。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.IO;

namespace NetworkCredential
{
    class Program
    {
        /// <summary>
        /// ファイル共有されたファイルをローカルにコピーする
        /// ただし、認証されていない場合は、WNetAddConnection2
        /// で認証されてからローカルにファイルをコピーする
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            string destFilePath = @"C:\test.txt";  // コピー先のローカルパス
            string sourceFilePath = @"\\sharePC\shareName\test.txt";  // コピー対象の共有されたファイルのUNCパス
            string shareName = @"\\sharePC\shareName"; // 共有パス \\sparePC\C$などもOK
            try
            {
                System.IO.File.Delete(destFilePath);
                System.IO.File.Copy(sourceFilePath, destFilePath);
            }
            catch (IOException)
            {
                // エラーが出た場合は接続情報を設定
                NETRESOURCE netResource = new NETRESOURCE();
                netResource.dwScope = 0;
                netResource.dwType = 1;
                netResource.dwDisplayType = 0;
                netResource.dwUsage = 0;
                netResource.lpLocalName = ""; // ネットワークドライブにする場合は"z:"などドライブレター設定
                   netResource.lpRemoteName = shareName;
                netResource.lpProvider = "";

                string password = "Admin";
                string userId = @"sharePC\Administrator";

                int ret = 0;
                try
                {
                    ret = WNetAddConnection2(ref netResource, password, userId, 0);
                }
                catch (Exception)
                {
                }
                Console.WriteLine(ret);
                // もう一回だけチャレンジ
                System.IO.File.Delete(destFilePath);
                System.IO.File.Copy(sourceFilePath, destFilePath);

            }
            Console.WriteLine("終了");
            Console.ReadLine();
        }

        [DllImport("mpr.dll", EntryPoint = "WNetAddConnection2",
        CharSet = System.Runtime.InteropServices.CharSet.Unicode)]
        private static extern int WNetAddConnection2(
        ref NETRESOURCE lpNetResource, string lpPassword, string lpUsername, Int32 dwFlags);

        [StructLayout(LayoutKind.Sequential)]
        internal struct NETRESOURCE
        {
            public int dwScope;
            public int dwType;
            public int dwDisplayType;
            public int dwUsage;
            [MarshalAs(UnmanagedType.LPWStr)]
            public string lpLocalName;
            [MarshalAs(UnmanagedType.LPWStr)]
            public string lpRemoteName;
            [MarshalAs(UnmanagedType.LPWStr)]
            public string lpComment;
            [MarshalAs(UnmanagedType.LPWStr)]
            public string lpProvider;
        }

    }
}

WNetAddConnection2にはWNetAddConnection3などがあります。MSDNを参照して下さい。細かいパラメタもMSDNを参照すれば確認できます。

ネットワークドライブを割り当てたい場合は

netResource.lpLocalName = "Z:"

のようにすれば割り当てることができるようになります。

説明は以上です。

誤り、指摘とうがあればご連絡ください。