網路磁碟機 upload & download

網路磁碟機 upload & download

動態建立網路磁碟機上傳與下載

net use (net use指令中的路徑若有空白字元, 如\\pcName\test file, 那麼必須將該路徑已雙引號框住, 如"\\pcName\test file")
net use 語法:
      建立網路磁碟機
      net use "\\pcName\test file"  password    /user:account
      移除網路磁碟機
      net use "\\pcName\test file" /delete

using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.IO;

namespace UploadAndDownload
{
    class Program
    {
        public bool Connect(string remoteHost, string userName, string password)
        {
            bool result = true;
            Process proc = new Process();
            proc.StartInfo.FileName = "cmd.exe";
            proc.StartInfo.UseShellExecute = false;
            proc.StartInfo.RedirectStandardInput = true;
            proc.StartInfo.RedirectStandardOutput = true;
            proc.StartInfo.RedirectStandardError = true;
            proc.StartInfo.CreateNoWindow = true;

            try
            {
                proc.Start();
                string command = "net  use  \"" + remoteHost + "\"  " + password + "  " + "  /user:" + userName;// + ">NUL"
                proc.StandardInput.WriteLine(command);
                command = "exit";
                proc.StandardInput.WriteLine(command);

                while (proc.HasExited == false)
                {
                    proc.WaitForExit(1000);
                }

                string errorMsg = proc.StandardError.ReadToEnd();
                if (errorMsg != "")
                    result = false;

                proc.StandardError.Close();
            }
            catch (Exception ex)
            {
                result = false;
                throw ex;
            }
            finally
            {
                proc.Close();
                proc.Dispose();
            }
            return result;
        }

        public bool DisConnect(string remoteHost)
        {
            bool result = true;
            Process proc = new Process();
            proc.StartInfo.FileName = "cmd.exe";
            proc.StartInfo.UseShellExecute = false;
            proc.StartInfo.RedirectStandardInput = true;
            proc.StartInfo.RedirectStandardOutput = true;
            proc.StartInfo.RedirectStandardError = true;
            proc.StartInfo.CreateNoWindow = true;

            try
            {

                proc.Start();
                string command = "net  use  \"" + remoteHost + "\" /delete";
                proc.StandardInput.WriteLine(command);
                command = "exit";
                proc.StandardInput.WriteLine(command);

                while (proc.HasExited == false)
                {
                    proc.WaitForExit(1000);
                }

                string errorMsg = proc.StandardError.ReadToEnd();
                if (errorMsg != "")
                    result = false;

                proc.StandardError.Close();
            }
            catch (Exception ex)
            {
                result = false;
                throw ex;
            }
            finally
            {
                proc.Close();
                proc.Dispose();
            }
            return result;
        }

        public bool FileCopy(string sourceFilePath, string sourceFileName, string userName, string password, string destinationFilePath, string destinationFileName)
        {
            try
            {
                bool result = false;

                result = Connect(destinationFilePath, userName, password);

                if (sourceFileName.Length > 0)
                {
                    File.Copy(sourceFilePath + "\\" + sourceFileName, destinationFilePath + "\\" + destinationFileName, true);
                }
                else
                {
                    DirectoryInfo source = new DirectoryInfo(sourceFilePath);
                    FileInfo[] fileInfo = source.GetFiles();

                    foreach (FileInfo f in fileInfo)
                    {
                        File.Copy(f.FullName, destinationFilePath + "\\" + destinationFileName, true);
                    }
                }

                result = DisConnect(destinationFilePath);
                return result;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        static void Main(string[] args)
        {
            Program p = new Program();

            string sourcePath = @"D:\test";
            string destinationPath = @"\\PC_Name\test";

            bool result = false;
            try
            {
                result = p.FileCopy(sourcePath, "source file name", "user name", "password", destinationPath, "destination file name",);
                if (!result)
                {
                     //失敗處理
                }
            }
            catch (Exception ex)
            {
                 //失敗處理
            }
        }
    }
}

參考:http://www.dotblogs.com.tw/dennismao/archive/2011/02/24/21550.aspx

 

使用 Microsoft.VisualBasic.Devices.Network 上傳與下載

Microsoft.VisualBasic.Devices.Network network = new Microsoft.VisualBasic.Devices.Network();
network.UploadFile(sourcePath + fileName, destinationPath + fileName, userName, password);