I’m trying to make sure that the user doesn’t download a file that will replace an existing downloaded (same) file. Hence, I tried to create YesNo dialog for the user to confirm. However, even if there’s no “sample.xml” in that folder, the YesNo dialog will still appear when the user click on Download button. May I know where did I do wrongly? I am lacking programming knowledge, sorry and please bear with me.
My code as below:
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!");
}
}
You’re checking the file path as a string to see if it’s null.. it’s never null, because it has the file path.
You’ll need to use
File.Exists(). Instead of this:Use this: