C# WebClient 多次执行
C# WebClient multiple execution
我的 WebClient 有问题。
简单我检查一个文件夹中丢失的文件。如果我没有这个文件,我会使用 WebClient 导航到网页并发送一个值来执行查询并将该值存储在数据库中。
我的问题:
例如,我有一个包含 1500 个元素的列表。
但是在第一个元素之后 for 循环停止(可能)或者不再导航。
我的代码:
List<string> fileneed = new List<string>();
在线程中
//Distinct
fileneed = fileneed.Distinct().ToList<string>();
for (int i = 0; i < fileneed.Count; i++)
{
if (fileneed[i].Contains("."))
{
w = new WebClient();
w.OpenRead("http://mywebsite.org/collab/files.php?act=need&user=" + Properties.Settings.Default.user + "&file=" + fileneed[i]);
fileneed.RemoveAt(i);
}
}
线程执行后,我转到我的 PhpMyAdmin,但只看到一个文件。
列表中的其他文件不显示或存在或存在奇怪问题,我的代码循环执行一次。
您正在打开该文件的 'connection',但您并没有阅读或存储它。您需要创建一个新文件,并从远程流读取并写入本地文件流:
using(var myFile = File.OpenWrite(fileneed[i]))
{
w.CopyTo(myFile);
}
详情见this page
我不知道这个页面到底做了什么,但你应该删除这一行;
fileneed.RemoveAt(i);
每次迭代,您都会删除元素和 Count
更改。如果你想删除处理过的项目,你可以存储在另一个列表中,除了原始字符串列表之外。
示例代码有一些错误:
1st:因为它在从列表中读取的同一点从 fileneed
列表中删除项目,所以它将跳过列表中的文件。这是因为当你删除一个项目时,后面所有项目的索引都会变小一个。我们可以通过从头到尾遍历列表来解决这个问题。
第二:虽然代码正在从服务器读取文件,但它没有对文件做任何事情以将其写出到磁盘。因此,文件只会丢失。这可以通过打开文件流并复制到它来解决。
3rd: WebClient
和从 OpenRead
返回的 Stream
需要是 Disposed. Otherwise the resources they use will not be cleaned up and your program will become a memory/connection hog. This is fixed by using the using
语句。
通过这三个修复,生成的代码如下所示:
fileneed = fileneed.Distinct().ToList<string>();
for (int i = fileneed.Count - 1; i >= 0; i--)
{
if (fileneed[i].Contains("."))
{
using (var w = new WebClient())
using (var webFile = w.OpenRead("http://mywebsite.org/collab/files.php?act=need&user=" + Properties.Settings.Default.user + "&file=" + fileneed[i]))
using (var diskFile = File.OpenWrite(fileneed[i]))
{
webFile.CopyTo(diskFile);
}
fileneed.RemoveAt(i);
}
}
我的 WebClient 有问题。
简单我检查一个文件夹中丢失的文件。如果我没有这个文件,我会使用 WebClient 导航到网页并发送一个值来执行查询并将该值存储在数据库中。
我的问题:
例如,我有一个包含 1500 个元素的列表。 但是在第一个元素之后 for 循环停止(可能)或者不再导航。
我的代码:
List<string> fileneed = new List<string>();
在线程中
//Distinct
fileneed = fileneed.Distinct().ToList<string>();
for (int i = 0; i < fileneed.Count; i++)
{
if (fileneed[i].Contains("."))
{
w = new WebClient();
w.OpenRead("http://mywebsite.org/collab/files.php?act=need&user=" + Properties.Settings.Default.user + "&file=" + fileneed[i]);
fileneed.RemoveAt(i);
}
}
线程执行后,我转到我的 PhpMyAdmin,但只看到一个文件。 列表中的其他文件不显示或存在或存在奇怪问题,我的代码循环执行一次。
您正在打开该文件的 'connection',但您并没有阅读或存储它。您需要创建一个新文件,并从远程流读取并写入本地文件流:
using(var myFile = File.OpenWrite(fileneed[i]))
{
w.CopyTo(myFile);
}
详情见this page
我不知道这个页面到底做了什么,但你应该删除这一行;
fileneed.RemoveAt(i);
每次迭代,您都会删除元素和 Count
更改。如果你想删除处理过的项目,你可以存储在另一个列表中,除了原始字符串列表之外。
示例代码有一些错误:
1st:因为它在从列表中读取的同一点从 fileneed
列表中删除项目,所以它将跳过列表中的文件。这是因为当你删除一个项目时,后面所有项目的索引都会变小一个。我们可以通过从头到尾遍历列表来解决这个问题。
第二:虽然代码正在从服务器读取文件,但它没有对文件做任何事情以将其写出到磁盘。因此,文件只会丢失。这可以通过打开文件流并复制到它来解决。
3rd: WebClient
和从 OpenRead
返回的 Stream
需要是 Disposed. Otherwise the resources they use will not be cleaned up and your program will become a memory/connection hog. This is fixed by using the using
语句。
通过这三个修复,生成的代码如下所示:
fileneed = fileneed.Distinct().ToList<string>();
for (int i = fileneed.Count - 1; i >= 0; i--)
{
if (fileneed[i].Contains("."))
{
using (var w = new WebClient())
using (var webFile = w.OpenRead("http://mywebsite.org/collab/files.php?act=need&user=" + Properties.Settings.Default.user + "&file=" + fileneed[i]))
using (var diskFile = File.OpenWrite(fileneed[i]))
{
webFile.CopyTo(diskFile);
}
fileneed.RemoveAt(i);
}
}