C#服務(wù)器NFS共享文件夾搭建與上傳圖片文件的實(shí)現(xiàn)
C#Windows server2016服務(wù)器搭建NFS共享文件夾與C#上傳圖片到共享文件夾
nfs共享文件夾實(shí)現(xiàn)步驟
基于:Windows server2016,其他版本大同小異
安裝NFS組件(如果已安裝略過(guò))






在源服務(wù)器建立nfs文件夾共享










到此服務(wù)器創(chuàng)建NFS就完成了,接下來(lái)我們開(kāi)始程序上傳
使用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("照片為空,請(qǐng)選擇圖片");
return;
}
if (dr == DialogResult.OK)
{
txtFilePath = openFileDialogTemp.FileName;
}
if (txtFilePath.Trim() == "")
{
GLOBALS.msgbox("請(qǐng)選擇文件!");
return;
}
string filePath = this.txtFilePath.Text;
string uploadUrl = IISPATH + isPath + "/" + id + ".jpg";
try
{
File.Copy(filePath, uploadUrl);//復(fù)制文件夾下的所有文件、目錄到指定的文件夾
GLOBALS.msgbox("上傳成功!");
}
catch (Exception)
{
}
}使用這個(gè)方法之前,先打開(kāi)cmd窗口,用dos命令運(yùn)行是否正常
- 命令:打開(kāi)連接:net use \\IP地址\uploadImages$ 密碼/user:用戶名 注意:只有三個(gè)空格
- 刪除連接:net use \\IP地址\uploadImages$ 密碼/user:用戶名\del
net use錯(cuò)誤解決方案:
- 錯(cuò)誤號(hào)5,拒絕訪問(wèn):很可能你使用的用戶不是管理員權(quán)限的,先提升權(quán)限;
- 錯(cuò)誤號(hào)51,Windows無(wú)法找到網(wǎng)絡(luò)路徑:網(wǎng)絡(luò)有問(wèn)題;
- 錯(cuò)誤號(hào)53,找不到網(wǎng)絡(luò)路徑:ip地址錯(cuò)誤;目標(biāo)未開(kāi)機(jī);目標(biāo)lanmanserver服務(wù)未啟動(dòng);目標(biāo)有防火墻(端口過(guò)濾);
- 錯(cuò)誤號(hào)67,找不到網(wǎng)絡(luò)名:你的lanmanworkstation服務(wù)未啟動(dòng)或者目標(biāo)刪除了uploadImages$;
- 錯(cuò)誤號(hào)1219,提供的憑據(jù)與已存在的憑據(jù)集沖突:你已經(jīng)和對(duì)方建立了一個(gè)uploadImages$,請(qǐng)刪除再連;
- 錯(cuò)誤號(hào)1326,未知的用戶名或錯(cuò)誤密碼:原因很明顯了;
到此這篇關(guān)于C#服務(wù)器NFS共享文件夾搭建與上傳圖片文件的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)C#服務(wù)器搭建與上傳圖片文件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#控制臺(tái)基礎(chǔ) list<>初始化的兩種方法
這篇文章主要介紹了C#控制臺(tái)基礎(chǔ) list<>初始化的兩種方法,需要的朋友可以參考下2016-12-12
c# 實(shí)現(xiàn)康威生命游戲(細(xì)胞自動(dòng)機(jī))的示例
這篇文章主要介紹了c# 實(shí)現(xiàn)康威生命游戲(細(xì)胞自動(dòng)機(jī))的示例,幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下2021-02-02
C#實(shí)現(xiàn)讀取匿名對(duì)象屬性值的方法示例總結(jié)
C#緩存之SqlCacheDependency用法實(shí)例總結(jié)

