1、先把服务器上的路径设置成共享路径
2、代码如下
string fileName = "5002356611"; // 替换为你要查找的文件名 string aaa = @"E:\SAP图片"; // 替换为源文件夹的路径 string b = @"\\173.1.60.169\test";// 替换成实际的网络路径(该路径必须是共享文件夹) var state = ConnectState(b, "账号", "密码");//访问网路共享路径的账号密码 if (state == false) { //无法连接服务器 } CopyFileByFileName(fileName, aaa, b); string bwer = string.Empty; CloseState(b, out bwer); bool CopyFileByFileName(string fileName, string sourcePath, string destinationPath) { string[] matchingFiles = Directory.GetFiles(sourcePath, fileName + ".*"); if (matchingFiles.Length > 0) { string sourceFilePath = matchingFiles[0]; string destinationFilePath = Path.Combine(destinationPath, Path.GetFileName(sourceFilePath)); CopyFile(sourceFilePath, destinationFilePath); //Console.WriteLine($"文件已成功从 {sourceFilePath} 复制到 {destinationFilePath}"); } else { return false; // Console.WriteLine($"未找到匹配的文件"); } return true; } void CopyFile(string sourcePath, string destinationPath) { File.Copy(sourcePath, destinationPath, true); } public void CopyFolder(string sourceFolder, string destFolder) { //如果目标路径不存在,则创建目标路径 if (!Directory.Exists(destFolder)) { Directory.CreateDirectory(destFolder); } //得到原文件根目录下的所有文件 string[] files = Directory.GetFiles(sourceFolder); foreach (string file in files) { string name = Path.GetFileName(file); string dest = Path.Combine(destFolder, name); File.Copy(file, dest);//复制文件 //if (files.Count() != 0) //{ // progressBar.Value += 1; //} } //得到原文件根目录下的所有文件夹 string[] folders = Directory.GetDirectories(sourceFolder); foreach (string folder in folders) { string name = Path.GetFileName(folder); string dest = Path.Combine(destFolder, name); CopyFolder(folder, dest); } } /// <summary> /// 连接远程共享文件夹 /// </summary> /// <param name="path">远程共享文件夹的路径</param> /// <param name="userName">用户名</param> /// <param name="passWord">密码</param> /// <returns></returns> private 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> /// 关闭网络路径的连接 /// </summary> /// <param name="path">网络路径</param> /// <param name="strErr"></param> /// <returns></returns> public bool CloseState(string path, out string strErr) { bool Flag = false; Process proc = new Process(); strErr = ""; try { proc.StartInfo.WorkingDirectory = Environment.CurrentDirectory; 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(); path = "\"" + path + "\""; string dosLine = @"net use " + path + " /d /y"; proc.StandardInput.WriteLine(dosLine); proc.StandardInput.WriteLine("exit"); while (!proc.HasExited) { proc.WaitForExit(1000); } string errMsg = proc.StandardError.ReadToEnd(); proc.StandardError.Close(); if (string.IsNullOrEmpty(errMsg)) { Flag = true; } else { throw new Exception(errMsg); } } catch (Exception ex) { strErr = ex.Message; } finally { proc.Close(); proc.Dispose(); } return Flag; }
标签:string,c#,true,路径,Flag,传到,StartInfo,服务器,proc From: https://www.cnblogs.com/kelenote/p/17902764.html