C#服務器NFS共享文件夾搭建與上傳圖片文件的實現(xiàn)
更新時間:2022年07月22日 11:32:24 作者:櫻花花
本文主要介紹了C#服務器NFS共享文件夾搭建與上傳圖片文件的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
C#Windows server2016服務器搭建NFS共享文件夾與C#上傳圖片到共享文件夾
nfs共享文件夾實現(xiàn)步驟
基于:Windows server2016,其他版本大同小異
安裝NFS組件(如果已安裝略過)






在源服務器建立nfs文件夾共享










到此服務器創(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>
/// 選擇圖片或文件上傳至服務器nfs共享文件夾
/// </summary>
public void DosCopyImage()
{
if (status == false)
{
GLOBALS.msgbox("連續(xù)服務器失敗!");
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);//復制文件夾下的所有文件、目錄到指定的文件夾
GLOBALS.msgbox("上傳成功!");
}
catch (Exception)
{
}
}使用這個方法之前,先打開cmd窗口,用dos命令運行是否正常
- 命令:打開連接:net use \\IP地址\uploadImages$ 密碼/user:用戶名 注意:只有三個空格
- 刪除連接:net use \\IP地址\uploadImages$ 密碼/user:用戶名\del
net use錯誤解決方案:
- 錯誤號5,拒絕訪問:很可能你使用的用戶不是管理員權限的,先提升權限;
- 錯誤號51,Windows無法找到網(wǎng)絡路徑:網(wǎng)絡有問題;
- 錯誤號53,找不到網(wǎng)絡路徑:ip地址錯誤;目標未開機;目標lanmanserver服務未啟動;目標有防火墻(端口過濾);
- 錯誤號67,找不到網(wǎng)絡名:你的lanmanworkstation服務未啟動或者目標刪除了uploadImages$;
- 錯誤號1219,提供的憑據(jù)與已存在的憑據(jù)集沖突:你已經(jīng)和對方建立了一個uploadImages$,請刪除再連;
- 錯誤號1326,未知的用戶名或錯誤密碼:原因很明顯了;
到此這篇關于C#服務器NFS共享文件夾搭建與上傳圖片文件的實現(xiàn)的文章就介紹到這了,更多相關C#服務器搭建與上傳圖片文件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

