我有一个listview控件和一个按钮的应用程序。 当我按下按钮时,我希望它将文件的时间和名称添加到列表视图中,然后将其添加到控件中,一旦完成下载文件,我希望它将另一个子项添加到同一列表视图中,但由于某种原因它没有添加它我无法弄清楚为什么,我说错了什么?
using System;
using System.ComponentModel;
using System.Net;
using System.Windows.Forms;
namespace Listviews
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
ListViewItem lvi = new ListViewItem();
WebClient webClient;
public void DownloadFile(string urlAddress, string location)
{
using (webClient = new WebClient())
{
webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
try
{
// Start downloading the file
webClient.DownloadFileAsync(new System.Uri(urlAddress), location);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
// The event that will trigger when the WebClient is completed
private void Completed(object sender, AsyncCompletedEventArgs e)
{
if (e.Cancelled == true)
{
MessageBox.Show("Download has been canceled.");
}
else
{
ListViewItem lvItems = new ListViewItem();
lvItems.SubItems.Add("Done");
listView1.Items.Add(lvItems);
}
}
private void btnAdd_Click(object sender, EventArgs e)
{
ListViewItem lvItems = new ListViewItem();
string clock = DateTime.Now.ToString("HH:mm");
lvItems.Text = clock;
listView1.Items.Add(lvItems);
string requestUrl = "url";
string fileName = "\\Name";
string savePath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
string combinedPaths = savePath + fileName;
DownloadFile(requestUrl, combinedPaths);
}
}
}