C#服務(wù)器NFS共享文件夾搭建與上傳圖片文件的實現(xiàn)
C#Windows server2016服務(wù)器搭建NFS共享文件夾與C#上傳圖片到共享文件夾
nfs共享文件夾實現(xiàn)步驟
基于:Windows server2016,其他版本大同小異
安裝NFS組件(如果已安裝略過)
在源服務(wù)器建立nfs文件夾共享
到此服務(wù)器創(chuàng)建NFS就完成了,接下來我們開始程序上傳
使用net dos命令
嘗試連接共享文件夾
bool status = connectState(@"\\IP\uploadImages", "用戶登錄名", "登錄密碼");
/// <summary> /// 連接共享文件 /// </summary> /// <param name="path">共享文件地址</param> /// <param name="userName">用戶名</param> /// <param name="passWord">密碼</param> /// <returns>true:連接成功 false:連接失敗</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(); 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; }
/// <summary> /// 選擇圖片或文件上傳至服務(wù)器nfs共享文件夾 /// </summary> public void DosCopyImage() { if (status == false) { GLOBALS.msgbox("連續(xù)服務(wù)器失敗!"); return; } string id = DateTime.Now.ToString("yyyyMMddHHmmss"); string isPath = DateTime.Now.ToString("yyyy-MM-dd"); string Path = IISPATH + isPath; if (!Directory.Exists(IISPATH)) { Directory.CreateDirectory(IISPATH); } string txtFilePath = ""; OpenFileDialog openFileDialogTemp = new OpenFileDialog(); openFileDialogTemp.Title = "選擇要上傳的圖片"; openFileDialogTemp.Filter = "*.jpg,*.png,|*.jpg;*.png;";//如需上傳文件把文件添加上即可 DialogResult dr = openFileDialogTemp.ShowDialog(); if (!File.Exists(openFileDialogTemp.FileName)) { GLOBALS.msgbox("照片為空,請選擇圖片"); return; } if (dr == DialogResult.OK) { txtFilePath = openFileDialogTemp.FileName; } if (txtFilePath.Trim() == "") { GLOBALS.msgbox("請選擇文件!"); return; } string filePath = this.txtFilePath.Text; string uploadUrl = IISPATH + isPath + "/" + id + ".jpg"; try { File.Copy(filePath, uploadUrl);//復(fù)制文件夾下的所有文件、目錄到指定的文件夾 GLOBALS.msgbox("上傳成功!"); } catch (Exception) { } }
使用這個方法之前,先打開cmd窗口,用dos命令運行是否正常
- 命令:打開連接:net use \\IP地址\uploadImages$ 密碼/user:用戶名 注意:只有三個空格
- 刪除連接:net use \\IP地址\uploadImages$ 密碼/user:用戶名\del
net use錯誤解決方案:
- 錯誤號5,拒絕訪問:很可能你使用的用戶不是管理員權(quán)限的,先提升權(quán)限;
- 錯誤號51,Windows無法找到網(wǎng)絡(luò)路徑:網(wǎng)絡(luò)有問題;
- 錯誤號53,找不到網(wǎng)絡(luò)路徑:ip地址錯誤;目標(biāo)未開機;目標(biāo)lanmanserver服務(wù)未啟動;目標(biāo)有防火墻(端口過濾);
- 錯誤號67,找不到網(wǎng)絡(luò)名:你的lanmanworkstation服務(wù)未啟動或者目標(biāo)刪除了uploadImages$;
- 錯誤號1219,提供的憑據(jù)與已存在的憑據(jù)集沖突:你已經(jīng)和對方建立了一個uploadImages$,請刪除再連;
- 錯誤號1326,未知的用戶名或錯誤密碼:原因很明顯了;
到此這篇關(guān)于C#服務(wù)器NFS共享文件夾搭建與上傳圖片文件的實現(xiàn)的文章就介紹到這了,更多相關(guān)C#服務(wù)器搭建與上傳圖片文件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章

C#實現(xiàn)讀取匿名對象屬性值的方法示例總結(jié)

C#緩存之SqlCacheDependency用法實例總結(jié)