C#把文件上傳到服務(wù)器中的指定地址
更新時間:2022年04月23日 11:40:22 作者:農(nóng)碼一生
這篇文章介紹了C#實現(xiàn)文件上傳到服務(wù)器指定地址的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
一、建立連接
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#實現(xiàn)文件上傳到服務(wù)器指定地址的文章就介紹到這了。希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
教你C#將CSV轉(zhuǎn)為Excel的實現(xiàn)方法
這篇文章主要介紹了C#?將CSV轉(zhuǎn)為Excel,轉(zhuǎn)換之后可執(zhí)行更多關(guān)于數(shù)據(jù)編輯、格式設(shè)置等操作,代碼簡單易懂,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧2022-03-03C#.NET中如何批量插入大量數(shù)據(jù)到數(shù)據(jù)庫中
這篇文章主要給大家介紹C#.net中如何批量插入大量數(shù)據(jù)到數(shù)據(jù)庫中,本文涉及到C#.net中批量插入數(shù)據(jù)到數(shù)據(jù)庫中方面的內(nèi)容,對C#.net批量插入數(shù)據(jù)到數(shù)據(jù)庫中感興趣的朋友可以參考下本篇文章2015-10-10C#使用遠(yuǎn)程服務(wù)調(diào)用框架Apache Thrift
這篇文章介紹了C#使用遠(yuǎn)程服務(wù)調(diào)用框架Apache Thrift的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-06-06Winform利用分頁控件實現(xiàn)導(dǎo)出PDF文檔功能
當(dāng)前的Winform分頁控件中,當(dāng)前導(dǎo)出的數(shù)據(jù)一般使用Excel來處理,但是有框架的使用客戶希望分頁控件能夠直接導(dǎo)出PDF,所以本文整理了一下分頁控件導(dǎo)出PDF的處理過程,分享一下2023-03-03C#實現(xiàn)讓窗體永遠(yuǎn)在窗體最前面顯示的實例
這篇文章主要介紹了C#實現(xiàn)讓窗體永遠(yuǎn)在窗體最前面顯示,功能非常實用,需要的朋友可以參考下2014-07-07