1. 程式人生 > >c#將本地檔案上傳至伺服器(內網)

c#將本地檔案上傳至伺服器(內網)

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using Aspose.Pdf; using System.IO; using System.Diagnostics;

namespace WindowsFormsApplication1 {     public partial class Form1 : Form     {         public Form1()         {             InitializeComponent();         }

        private void button1_Click(object sender, EventArgs e)         {             UploadECCInvoice_Intranet("");         }         /// <summary>         /// 內網上傳ECC Invoice         /// </summary>         /// <param name="path_ECCInvoice">ECCInvoice的路徑</param>         private void UploadECCInvoice_Intranet(string path_ECCInvoice)         {             string destinationFile = @"E:\test\DL180226H012.pdf";//要上傳的檔案             bool status = false;             //連線               string serverFolder = @"\\192.168.1.188\Evan";             string PWD = "

[email protected]#";//kingdee1!             status = connectState(serverFolder, "Administrator", PWD);             if (status)             {                 //共享資料夾的目錄                   DirectoryInfo theFolder = new DirectoryInfo(serverFolder);                 string filename = theFolder.ToString();                 //執行方法   

                TransportRemoteToServer(serverFolder + @"\", destinationFile, "test.pdf");    //實現將遠端伺服器檔案寫入到本地  

            }             else             {                 MessageBox.Show("連線伺服器失敗!");             }

        }         /// <summary>           /// 從本地上傳檔案至伺服器         /// </summary>           /// <param name="src">遠端伺服器路徑(共享資料夾路徑)</param>           /// <param name="dst">本地資料夾路徑</param>           /// <param name="fileName">上傳至伺服器上的檔名,包含副檔名</param>           public static void TransportRemoteToServer(string src, string dst, string fileName)         {             if (!Directory.Exists(dst))             {                 Directory.CreateDirectory(dst);             }             src = src + fileName;             FileStream inFileStream = new FileStream(src, FileMode.OpenOrCreate);    //從遠端伺服器下載到本地的檔案 

            FileStream outFileStream = new FileStream(dst, FileMode.Open);    //遠端伺服器檔案  此處假定遠端伺服器共享資料夾下確實包含本檔案,否則程式報錯  

            byte[] buf = new byte[outFileStream.Length];

            int byteCount;

            while ((byteCount = outFileStream.Read(buf, 0, buf.Length)) > 0)             {

                inFileStream.Write(buf, 0, byteCount);

            }

            inFileStream.Flush();

            inFileStream.Close();

            outFileStream.Flush();

            outFileStream.Close();

        }

        /// <summary>           /// 連線遠端共享資料夾           /// </summary>           /// <param name="path">遠端共享資料夾的路徑</param>           /// <param name="userName">使用者名稱</param>           /// <param name="passWord">密碼</param>           /// <returns></returns>           public static bool connectState(string path, string userName, string passWord)         {             bool Flag = false;             Process proc = new Process();             try             {                 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;                 proc.Start();                 proc.StandardInput.WriteLine("net use * /del /y");                 string dosLine = "net use " + path + " " + passWord + " /user:" + userName;                 proc.StandardInput.WriteLine(dosLine);                 proc.StandardInput.WriteLine("exit");                 while (!proc.HasExited)                 {                     proc.WaitForExit(1000);                 }                 string errormsg = proc.StandardError.ReadToEnd();                 proc.StandardError.Close();                 if (string.IsNullOrEmpty(errormsg))                 {                     Flag = true;                 }                 else                 {                     throw new Exception(errormsg);                 }             }             catch (Exception ex)             {                 throw ex;             }             finally             {                 proc.Close();                 proc.Dispose();             }             return Flag;         }     } }