在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
控制台应用程序 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using System.Diagnostics; namespace test3 { class Program { static void Main(string[] args) { bool status = false; //连接共享目录 status = connectState(@"\\10.0.xx.xxx\ctec2", "waveaccess", "waveaccessxxx"); if (status) { //共享文件夹的目录 DirectoryInfo theFolder = new DirectoryInfo(@"\\10.0.xx.xxx\ctec2\"); string filename = theFolder.ToString(); //执行方法 TransportLocalToRemote(@"D:\readme1.txt", filename, "readme1.txt"); //实现将本地文件写入到远程服务器 TransportRemoteToLocal(@"D:\readme.txt", filename, "readme.txt"); //实现将远程服务器文件写入到本地 } else { Console.WriteLine("未能连接!"); } Console.WriteLine("成功"); Console.ReadKey(); } public static bool connectState(string path) { return connectState(path, "", ""); } /// <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 " + 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="src">下载到本地后的文件路径,包含文件的扩展名</param> /// <param name="dst">远程服务器路径(共享文件夹路径)</param> /// <param name="fileName">远程服务器(共享文件夹)中的文件名称,包含扩展名</param> public static void TransportRemoteToLocal(string src, string dst, string fileName) //src:下载到本地后的文件路径 dst:远程服务器路径 fileName:远程服务器dst路径下的文件名 { if (!Directory.Exists(dst)) { Directory.CreateDirectory(dst); } dst = dst + fileName; FileStream inFileStream = new FileStream(dst, FileMode.Open); //远程服务器文件 此处假定远程服务器共享文件夹下确实包含本文件,否则程序报错 FileStream outFileStream = new FileStream(src, FileMode.OpenOrCreate); //从远程服务器下载到本地的文件 byte[] buf = new byte[inFileStream.Length]; int byteCount; while ((byteCount = inFileStream.Read(buf, 0, buf.Length)) > 0) { outFileStream.Write(buf, 0, byteCount); } inFileStream.Flush(); inFileStream.Close(); outFileStream.Flush(); outFileStream.Close(); } /// <summary> /// 将本地文件上传到远程服务器共享目录 /// </summary> /// <param name="src">本地文件的绝对路径,包含扩展名</param> /// <param name="dst">远程服务器共享文件路径,不包含文件扩展名</param> /// <param name="fileName">上传到远程服务器后的文件扩展名</param> public static void TransportLocalToRemote(string src, string dst, string fileName) //src { FileStream inFileStream = new FileStream(src, FileMode.Open); //此处假定本地文件存在,不然程序会报错 if (!Directory.Exists(dst)) //判断上传到的远程服务器路径是否存在 { Directory.CreateDirectory(dst); } dst = dst + fileName; //上传到远程服务器共享文件夹后文件的绝对路径 FileStream outFileStream = new FileStream(dst, FileMode.OpenOrCreate); byte[] buf = new byte[inFileStream.Length]; int byteCount; while ((byteCount = inFileStream.Read(buf, 0, buf.Length)) > 0) { outFileStream.Write(buf, 0, byteCount); } inFileStream.Flush(); inFileStream.Close(); outFileStream.Flush(); outFileStream.Close(); } } }
|
请发表评论