C#向共享文件夹上传及下载文件
2012-08-01 12:26
423 查看
//下载文件
public void DownLoadFile(string URL, string DIR)
{
WebClient client = new WebClient();
string FileName = URL.Substring(URL.LastIndexOf("\\") + 1);
string PATH = DIR + FileName;
try
{
WebRequest SC = WebRequest.Create(URL);
}
catch
{
}
try
{
client.DownloadFile(URL, PATH);
}
catch
{
}
}
//上传文件:要设置共享文件夹是否有创建的权限,否则无法上传文件
public void UpLoadFile(string fileNamePath, string urlPath,string User,string Pwd)
{
string newFileName = fileNamePath.Substring(fileNamePath.LastIndexOf(@"\") + 1);//取文件名称
MessageBox.Show(newFileName);
if (urlPath.EndsWith(@"\") == false) urlPath = urlPath + @"\";
urlPath = urlPath + newFileName;
WebClient myWebClient = new WebClient();
NetworkCredential cread = new NetworkCredential(User, Pwd, "Domain");
myWebClient.Credentials = cread;
FileStream fs = new FileStream(fileNamePath, FileMode.Open, FileAccess.Read);
BinaryReader r = new BinaryReader(fs);
try
{
byte[] postArray = r.ReadBytes((int)fs.Length);
Stream postStream = myWebClient.OpenWrite(urlPath);
// postStream.m
if (postStream.CanWrite)
{
postStream.Write(postArray, 0, postArray.Length);
MessageBox.Show("文件上传成功!","提醒",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
else
{
MessageBox.Show("文件上传错误!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
postStream.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "错误");
}
}
public void DownLoadFile(string URL, string DIR)
{
WebClient client = new WebClient();
string FileName = URL.Substring(URL.LastIndexOf("\\") + 1);
string PATH = DIR + FileName;
try
{
WebRequest SC = WebRequest.Create(URL);
}
catch
{
}
try
{
client.DownloadFile(URL, PATH);
}
catch
{
}
}
//上传文件:要设置共享文件夹是否有创建的权限,否则无法上传文件
public void UpLoadFile(string fileNamePath, string urlPath,string User,string Pwd)
{
string newFileName = fileNamePath.Substring(fileNamePath.LastIndexOf(@"\") + 1);//取文件名称
MessageBox.Show(newFileName);
if (urlPath.EndsWith(@"\") == false) urlPath = urlPath + @"\";
urlPath = urlPath + newFileName;
WebClient myWebClient = new WebClient();
NetworkCredential cread = new NetworkCredential(User, Pwd, "Domain");
myWebClient.Credentials = cread;
FileStream fs = new FileStream(fileNamePath, FileMode.Open, FileAccess.Read);
BinaryReader r = new BinaryReader(fs);
try
{
byte[] postArray = r.ReadBytes((int)fs.Length);
Stream postStream = myWebClient.OpenWrite(urlPath);
// postStream.m
if (postStream.CanWrite)
{
postStream.Write(postArray, 0, postArray.Length);
MessageBox.Show("文件上传成功!","提醒",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
else
{
MessageBox.Show("文件上传错误!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
postStream.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "错误");
}
}
相关文章推荐
- C#向其它电脑共享文件夹上传及下载文件
- JAVA从局域网共享文件夹中下载上传文件以及java访问共享文件夹
- C# FTP 文件 文件夹操作 上传 下载
- C#实现多文件上传,写到文件夹中,获取文件信息以及下载文件和删除文件
- C# FTP 文件 文件夹操作 上传 下载
- C# 之 FTP服务器中文件上传与下载(三)
- C#.net+JS文件批量上传解决方案附下载
- 如何像拖拽文件夹一样向服务器上传下载文件
- C#上传文件,读取文件夹和文件
- C#访问远程服务器共享目录下载文件到本地
- java上传文件到共享文件夹
- 文件夹没有安全选项-文件上传下载-路径访问被拒绝
- [上传下载] C#FileDown文件下载类 (转载)
- C#实现文件上传下载
- <php+mysql>上传文件到指定文件夹,共享数据和脚本,文件验证,脚本之间的通信
- C#实现文件上传下载Excel文档示例代码
- 在Xshell中上传下载文件到本地(linux中从多次ssh登录的dbserver里面的文件夹)
- C# 文件操作(上传 下载 删除 文件列表...)
- C# ASP.NET 文件上传下载 下载时图片不自动打开
- 基于SMB/JCIFS协议的共享文件上传和下载(局域网中共享文件获取文件)