我试图确保用户不会下载将替换现有下载(相同)文件的文件。因此,我尝试创建 YesNo 对话框供用户确认。但是,即使该文件夹中没有“sample.xml”,当用户单击下载按钮时,YesNo 对话框仍然会出现。我可以知道我在哪里做错了吗?我缺乏编程知识,对不起,请多多包涵。
我的代码如下:
private void btnDownloadXML2_Click(object sender, EventArgs e)
{
if (@"..\..\sharePriceXML\" + txtSharePriceSymbol.Text.ToString() + ".xml" != null)
{
DialogResult dialogResult = MessageBox.Show("Are you sure you want to re-download the file? This will replace the old file!", "Warning", MessageBoxButtons.YesNo);
if (dialogResult == DialogResult.Yes)
{
using (WebClient client = new WebClient())
{
client.DownloadFile("http://www.lse.co.uk/chat/" + txtSharePriceSymbol.Text.ToString(),
@"..\..\sharePriceXML\" + txtSharePriceSymbol.Text.ToString() + ".xml");
}
MessageBox.Show("Download Completed! File has been placed in the folder sharePriceXML!");
}
else if (dialogResult == DialogResult.No)
{
MessageBox.Show("The file is not downloaded. Your old file remains.");
}
}
else if (@"..\..\sharePriceXML\" + txtSharePriceSymbol.Text.ToString() + ".xml" == null)
{
using (WebClient client = new WebClient())
{
client.DownloadFile("http://www.lse.co.uk/chat/" + txtSharePriceSymbol.Text.ToString(),
@"..\..\sharePriceXML\" + txtSharePriceSymbol.Text.ToString() + ".xml");
}
MessageBox.Show("Download Completed! File has been placed in the folder sharePriceXML!");
}
}