c#中用Webclient类能否遍历文件夹下的所有文件,依次读取呢??

vicepaladin 2009-04-20 08:23:35
c#中用Webclient类能否遍历文件夹下的所有文件,依次读取呢??
例如

WebClient client1 = new WebClient();

byte[] buffer1 = client1.DownloadData("***");
能否通过某些语句依次遍历文件夹下的文件,依次downloaddata呢??
...全文
556 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
Xorcerer 2009-04-21
  • 打赏
  • 举报
回复
不行,因为Http没有文件夹的概念。
当然,有些服务器的确会返回某些路径下的文件列表,但是对于客户端来说,它也是以HTML页面的形式出现的。
wangping_li 2009-04-21
  • 打赏
  • 举报
回复
参考下面这个,这个就是我遍历文件夹下所有文件,进行下载的(我自动升级时用的)

private void DownUpdateFile()
{
try
{
mainAppExe = updaterXmlFiles.GetNodeValue("//EntryPoint");
Process[] allProcess = Process.GetProcesses();
foreach (Process p in allProcess)
{

if (p.ProcessName.ToLower() + ".exe" == mainAppExe.ToLower())
{
for (int i = 0; i < p.Threads.Count; i++)
p.Threads[i].Dispose();
p.Kill();
isRun = true;
}
}
//上面那些可以不用管,从下面开始看读取文件的

WebClient wcClient = new WebClient();
for (int i = 0; i < this.lvUpdateList.Items.Count; i++)
{
string UpdateFile = lvUpdateList.Items[i].Text.Trim();
string updateFileUrl = updateUrl + "/" + lvUpdateList.Items[i].Text.Trim();
long fileLength = 0;

WebRequest webReq = WebRequest.Create(updateFileUrl);
WebResponse webRes = webReq.GetResponse();
fileLength = webRes.ContentLength;

lbState.Text = "正在下载更新文件,请稍后...";
pbDownFile.Value = 0;
pbDownFile.Maximum = (int)fileLength;

try
{
Stream srm = webRes.GetResponseStream();
StreamReader srmReader = new StreamReader(srm);
byte[] bufferbyte = new byte[fileLength];
int allByte = (int)bufferbyte.Length;
int startByte = 0;
while (fileLength > 0)
{
Application.DoEvents();
int downByte = srm.Read(bufferbyte, startByte, allByte);
if (downByte == 0) { break; };
startByte += downByte;
allByte -= downByte;
pbDownFile.Value += downByte;

float part = (float)startByte / 1024;
float total = (float)bufferbyte.Length / 1024;
int percent = Convert.ToInt32((part / total) * 100);

this.lvUpdateList.Items[i].SubItems[2].Text = percent.ToString() + "%";

}

string tempPath = tempUpdatePath + UpdateFile;
CreateDirtory(tempPath);
FileStream fs = new FileStream(tempPath, FileMode.OpenOrCreate, FileAccess.Write);
fs.Write(bufferbyte, 0, bufferbyte.Length);
srm.Close();
srmReader.Close();
fs.Close();


}
catch (WebException ex)
{
MessageBox.Show("更新文件下载失败!" + ex.Message.ToString(), "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
InvalidateControl();
this.Cursor = Cursors.Default;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
ouyangyanyue 2009-04-21
  • 打赏
  • 举报
回复
额................晕
ouyangyanyue 2009-04-21
  • 打赏
  • 举报
回复


public static bool DownloadFile(string URL,string FilePath)
{
webClicent client1=new WebClient();
Stream WebStream;
try
{
webStream=client1.OpenRead(URL);
}
catch(Exception er1)
{
return false;
}
StreamReader reader=new StreamReader(WebStream);
string data=reader.ReadTOEnd();
try
{
FileStream localStream=new FileStream(FilePath,FileMode.Create,FileAccess.Write);
StreamWriter writer=new StreamWriter(localStream);
writer.Write(data);
writer.Close();
localStream.Close();
return true;
}
catch(Exception)
{
return false;
}
}
wuyq11 2009-04-20
  • 打赏
  • 举报
回复

110,567

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧