C# 向共享文件夹上传及下载文件

时间:2016-03-09 17:34:15   收藏:0   阅读:467
 1 public void DownLoadFile(string URL, string DIR)
 2         {
 3             WebClient client = new WebClient();
 4             string FileName = URL.Substring(URL.LastIndexOf("\\") + 1);
 5             string PATH = DIR + FileName;
 6             try
 7             {
 8                 WebRequest SC = WebRequest.Create(URL);
 9             }
10             catch
11             {
12             }
13             try
14             {
15                 client.DownloadFile(URL, PATH);
16             }
17             catch
18             {
19             }
20         }
 1 public void UpLoadFile(string fileNamePath, string urlPath,string User,string Pwd)
 2         {
 3             string newFileName = fileNamePath.Substring(fileNamePath.LastIndexOf(@"\") + 1);//取文件名称
 4             MessageBox.Show(newFileName);
 5             if (urlPath.EndsWith(@"\") == false) urlPath = urlPath + @"\";
 6 
 7             urlPath = urlPath + newFileName;
 8 
 9             WebClient myWebClient = new WebClient();
10             NetworkCredential cread = new NetworkCredential(User, Pwd, "Domain");
11             myWebClient.Credentials = cread;
12             FileStream fs = new FileStream(fileNamePath, FileMode.Open, FileAccess.Read);
13             BinaryReader r = new BinaryReader(fs);
14 
15             try
16             {
17                 byte[] postArray = r.ReadBytes((int)fs.Length);
18                 Stream postStream = myWebClient.OpenWrite(urlPath);
19                 // postStream.m
20                 if (postStream.CanWrite)
21                 {
22                     postStream.Write(postArray, 0, postArray.Length);
23                     MessageBox.Show("文件上传成功!","提醒",MessageBoxButtons.OK,MessageBoxIcon.Information);
24                 }
25                 else
26                 {
27                     MessageBox.Show("文件上传错误!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
28                 }
29 
30                 postStream.Close();
31             }
32             catch (Exception ex)
33             {
34                 MessageBox.Show(ex.Message, "错误");
35             }
36 
37         }

 

//上传文件:要设置共享文件夹是否有创建的权限,否则无法上传文件

 

原文:http://www.cnblogs.com/codedreams/p/5258776.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!