标签:style blog http color io os ar java for
最近接到一个新的项目,不通过http、ftp下载文件;通过获取共享文件,直接将文件拷贝到本地;方法如下:
1 WebClient wc = new WebClient(); 2 //url下载的web地址 3 //fileName 目标路径 4 wc.DownloadFile(url, fileName);
通过共享文件夹,获取共享文件,需解决以下问题:
步骤如下
try { GetAccessControl(textBox3.Text, textBox1.Text, textBox2.Text); } catch (Exception ex) { MessageBox.Show(ex.Message); } SaveFileDialog save = new SaveFileDialog(); //服务器共享文件夹下载地址 string frompath = @"\\192.168.50.180\aaa\MyEclipseGen.java"; //设置文件名 save.FileName = frompath.Substring(frompath.LastIndexOf("\\") + 1); if (save.ShowDialog() == DialogResult.OK) { CopyFile(frompath, save.FileName); }
/// <summary> /// 连接共享服务器 /// </summary> /// <param name="path">ip地址</param> /// <param name="user">用户名</param> /// <param name="pwd">密码</param> void GetAccessControl(string path, string user, string pwd) { Process p = new Process(); p.StartInfo.FileName = System.Environment.GetEnvironmentVariable("ComSpec"); p.StartInfo.UseShellExecute = false; p.StartInfo.RedirectStandardInput = true; p.StartInfo.RedirectStandardOutput = true; p.StartInfo.CreateNoWindow = true; p.Start(); p.StandardInput.WriteLine(@"Net Use \\{0} /del", path); //必须先删除,否则报错 p.StandardInput.WriteLine(@"Net Use \\{0} ""{1}"" /user:{2}", path, pwd, user); p.StandardInput.WriteLine("exit"); //如果不加这句WaitForExit会卡住 p.WaitForExit(); p.Close(); } /// <summary> /// 拷贝文件 /// </summary> /// <param name="frompath"></param> /// <param name="topath"></param> void CopyFile(string frompath, string topath) { try { File.Copy(frompath, topath, true); } catch (Exception ex) { MessageBox.Show(ex.Message); } }
标签:style blog http color io os ar java for
原文地址:http://www.cnblogs.com/tuqun/p/4030709.html