我将 xml 文件从 Internet 下载到内存手机。我想查看 Internet 连接是否可用于进行下载,如果没有则发送消息。如果不是,我想查看内存中是否已经存在 xml 文件。如果存在,则应用程序不会进行下载。
问题是我不知道如何制作“if”条件来查看文件是否存在。
我有这个代码:
public MainPage()
{
public MainPage()
{
if (NetworkInterface.GetIsNetworkAvailable())
{
InitializeComponent();
WebClient downloader = new WebClient();
Uri xmlUri = new Uri("http://dl.dropbox.com/u/32613258/file_xml.xml", UriKind.Absolute);
downloader.DownloadStringCompleted += new DownloadStringCompletedEventHandler(Downloaded);
downloader.DownloadStringAsync(xmlUri);
}
else
{
MessageBox.Show("The internet connection is not available");
}
}
void Downloaded(object sender, DownloadStringCompletedEventArgs e)
{
if (e.Result == null || e.Error != null)
{
MessageBox.Show("There was an error downloading the xml-file");
}
else
{
IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication();
var stream = new IsolatedStorageFileStream("xml_file.xml", FileMode.Create, FileAccess.Write, myIsolatedStorage);
using (StreamWriter writeFile = new StreamWriter(stream))
{
string xml_file = e.Result.ToString();
writeFile.WriteLine(xml_file);
writeFile.Close();
}
}
}
}
我不知道如何查看文件是否存在条件:(