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)
- {
-
- }
- }
使用这个方法之前,先打开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,提供的凭据与已存在的凭据集冲突:你已经和对方建立了一个uploadImages$,请删除再连;
错误号1326,未知的用户名或错误密码:原因很明显了;