C# NFS共享文件上传实现

nfs共享文件夹实现步骤
基于:Windows server2016,其他版本大同小异

安装NFS组件(如果已安装略过)
nfs共享文件夹实现步骤
基于:Windows server2016,其他版本大同小异

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

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("连续服务器失败!");
                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)
            {
 
            }
        }

安装NFS组件(如果已安装略过)
使用这个方法之前,先打开cmd窗口,用dos命令运行是否正常

命令:打开连接:net use \IP地址\uploadImages$ 密码/user:用户名 注意:只有三个空格
删除连接:net use \IP地址\uploadImages$ 密码/user:用户名\del
net use错误解决方案:

错误号5,拒绝访问:很可能你使用的用户不是管理员权限的,先提升权限;
错误号51,Windows无法找到网络路径:网络有问题;
错误号53,找不到网络路径:ip地址错误;目标未开机;目标lanmanserver服务未启动;目标有防火墙(端口过滤);
错误号67,找不到网络名:你的lanmanworkstation服务未启动或者目标删除了uploadImages ;错误号 1219 ,提供的凭据与已存在的凭据集冲突:你已经和对方建立了一个 u p l o a d I m a g e s ; 错误号1219,提供的凭据与已存在的凭据集冲突:你已经和对方建立了一个uploadImages ;错误号1219,提供的凭据与已存在的凭据集冲突:你已经和对方建立了一个uploadImages,请删除再连;
错误号1326,未知的用户名或错误密码:原因很明显了;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值