C#操作共享文件在两台电脑间进行读写

注塑机远程连接

两台电脑通过网线互联进行文件共享,请参考 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.100hfg";   //共享文件夹
            if (connectState(strPath0, "MES", "Ab123456"))
            {
                MessageBox.Show("成功");
            }
            else
            {
                MessageBox.Show("失败");
            }
        }

        private void btn_Read_Click(object sender, EventArgs e)
        {
            string strPath0 = @"\192.168.0.100hfg";   //共享文件夹
            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.100hfg";   //共享文件夹
            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();
            }
        }
代码
原文地址:https://www.cnblogs.com/yangmengke2018/p/12083447.html