溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

C#怎么把文件上傳到服務(wù)器中的指定地址

發(fā)布時(shí)間:2022-04-24 10:31:26 來(lái)源:億速云 閱讀:350 作者:iii 欄目:開(kāi)發(fā)技術(shù)

這篇文章主要介紹了C#怎么把文件上傳到服務(wù)器中的指定地址的相關(guān)知識(shí),內(nèi)容詳細(xì)易懂,操作簡(jiǎn)單快捷,具有一定借鑒價(jià)值,相信大家閱讀完這篇C#怎么把文件上傳到服務(wù)器中的指定地址文章都會(huì)有所收獲,下面我們一起來(lái)看看吧。

一、建立連接

        public string connectFTP(string vPath, string vUID, string vPassword)
        {
            string errormsg = "";
           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();
                string dosLine = "net use " + vPath + " " + vPassword + " /user:" + vUID;
                proc.StandardInput.WriteLine(dosLine);
                proc.StandardInput.WriteLine("exit");
                while (!proc.HasExited)
                {
                    proc.WaitForExit(1000);
                }
                errormsg = proc.StandardError.ReadToEnd();
                proc.StandardError.Close();
            }
            catch (Exception ex)
            {
                //throw ex;
                //MessageBox.Show(ex.Message);
            }
            finally
            {
                proc.Close();
                proc.Dispose();
            }
            return errormsg;
        }

二、上傳文件

        public void UploadFile(string vPath, string vUID, string vPassword, string vLocalPath, string file)
        {
            bool status = false;
            status = connectState(vPath, vUID, vPassword);
            if (status)
            {
                DirectoryInfo theFolder = new DirectoryInfo(vPath + "/" + file);
                string filename = vLocalPath;
                Transport(vLocalPath, vPath + "/" + file);
                //System.Diagnostics.Process.Start(vPath);
            }
            else
            {
                mesLog.Info("未能連接!");
                //MessageBox.Show("未能連接!");
            }
        }

三、連接狀態(tài)

        public static bool connectState(string vPath, string vUID, string vPassword)
        {
            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();
                string dosLine = "net use " + vPath + " " + vPassword + " /user:" + vUID;
                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;
                //MessageBox.Show(ex.Message);
            }
            finally
            {
                proc.Close();
                proc.Dispose();
            }
            return Flag;
        }

四、傳送

        public static void Transport(string src, string fileName)
        {
            FileStream inFileStream = new FileStream(src, FileMode.Open);
            FileStream outFileStream = new FileStream(fileName, FileMode.OpenOrCreate);

            byte[] buf = new byte[inFileStream.Length];
            int byteCount;

            while ((byteCount = inFileStream.Read(buf, 0, buf.Length)) > 0)
            {
                outFileStream.Write(buf, 0, byteCount);
            }

            inFileStream.Flush();
            inFileStream.Close();
            outFileStream.Flush();
            outFileStream.Close();

            File.Delete(src);
        }

關(guān)于“C#怎么把文件上傳到服務(wù)器中的指定地址”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對(duì)“C#怎么把文件上傳到服務(wù)器中的指定地址”知識(shí)都有一定的了解,大家如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI