注塑機遠程連接
兩台電腦通過網線互聯進行文件共享,請參考 https://blog.csdn.net/qq_38161654/article/details/80865241
1.設置電腦一,理解為客戶端
設置TCP/IPv4 IP地址和默認網關:192.168.0.10,子網掩碼:255.255.255.0
2.設置電腦二,理解為服務器,共享文件在此電腦上
設置TCP/IPv4 IP地址:192.168.0.100,子網掩碼:255.255.255.0,默認網關:192.168.0.10
3.Ping測試:cmd “ping 192.168.0.10”或“ping 192.168.0.100”
4.電腦而桌面設置一文件夾:命名hfg,右鍵屬性,然后點擊共享,再點擊高級共享 ,把共享此文件夾前面的方框勾上 ,設置共享的權限
可以設置允許修改寫入等開放文件夾權限
5.電腦一輸入:\\192.168.0.100 或 \\電腦二電腦名\hfg
-----------------------------------------------------------------------------------------------------
6.修改電腦二計算機名:MES-111
7.給電腦二添加管理員賬戶:MES,密碼:123456
8.編程連接的時候需要用到管理員賬戶和登錄密碼
代碼:

/// <summary> /// 判斷文本是否以\\結尾,如果不是在結尾添加\\ /// </summary> /// <param name="checkString"></param> /// <returns></returns> public static string BackSlash(string checkString) { string strTmp = ""; strTmp = checkString; try { if (!string.IsNullOrEmpty(checkString)) { if (strTmp[strTmp.Length - 1].ToString() != "\\") { strTmp += "\\"; } } else { strTmp = ""; } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex); } return strTmp; } /// <summary> /// 判斷連接狀態 /// </summary> /// <param name="path">共享文件夾</param> /// <param name="userName">管理員名字,不是計算機名</param> /// <param name="passWord">密碼</param> /// <returns></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 /delete * /yes " + System.Environment.NewLine; proc.StandardInput.WriteLine(dosLine); String dosLine2; dosLine2 = @"net use " + path + " /User:" + userName + " " + passWord + " /PERSISTENT:YES " + System.Environment.NewLine; proc.StandardInput.WriteLine(dosLine2); 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; } private void btn_conn_Click(object sender, EventArgs e) { string strPath0 = @"\\192.168.0.100\hfg"; //共享文件夾 if (connectState(strPath0, "MES", "Ab123456")) { MessageBox.Show("成功"); } else { MessageBox.Show("失敗"); } } private void btn_Read_Click(object sender, EventArgs e) { string strPath0 = @"\\192.168.0.100\hfg"; //共享文件夾 string destFn = "888.txt"; //要讀取共享文件夾下文件的名稱 string destFnNew = "888new.txt"; bool stt1 = connectState(strPath0, "MES", "Ab123456"); if (stt1) { string FullPath = BackSlash(strPath0) + destFn; string newPath = BackSlash(strPath0) + destFnNew; if (File.Exists(FullPath)) { File.Copy(FullPath, newPath); if (File.Exists(newPath)) { StreamReader sr = new StreamReader(newPath); string strTmp = sr.ReadToEnd(); sr.Close(); tb_2.Text = strTmp; File.Delete(FullPath); File.Delete(newPath); } } } } private void btn_Write_Click(object sender, EventArgs e) { //string txtPath = Directory.GetCurrentDirectory() + "\\111.txt"; //本地txt文件 string strPath0 = @"\\192.168.0.100\hfg"; //共享文件夾 string destFn = "888.txt"; //要在共享文件夾下生成的文件的名稱 bool stt1 = connectState(strPath0, "MES", "Ab123456"); if (stt1) { if (string.IsNullOrEmpty(tb_1.Text)) { MessageBox.Show("為空"); return; } //BackSlash(strPath0) + destFn是共享文件夾下要存儲的文件的絕對路徑 //就是把本地的txt文件放到另一台電腦共享文件夾下 FileStream fs = new FileStream(BackSlash(strPath0) + destFn, FileMode.Create); using (StreamWriter sw = new StreamWriter(fs)) { sw.Write(tb_1.Text.Trim()); sw.Flush(); sw.Close(); } fs.Close(); } }