0

我试图确保用户不会下载将替换现有下载(相同)文件的文件。因此,我尝试创建 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!");
    }
}
4

1 回答 1

3

您正在将文件路径作为字符串检查以查看它是否为空。它永远不会为空,因为它具有文件路径。

您将需要使用File.Exists(). 而不是这个:

if (@"..\..\sharePriceXML\" + txtSharePriceSymbol.Text.ToString() + ".xml" != null)

用这个:

if (File.Exists(@"..\..\sharePriceXML\" + txtSharePriceSymbol.Text.ToString() + ".xml")) 
于 2012-08-10T01:44:48.307 回答