1

インターネットからメモリフォンにxmlファイルをダウンロードします。ダウンロードを行うためにインターネット接続が利用可能かどうかを確認し、利用できない場合はメッセージを送信します。そして、そうでない場合は、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();
            }
        }
    }
}

ファイルが条件付きで存在するかどうかを確認する方法がわかりません:(

4

1 に答える 1

5

IsolatedStorageFileクラスには、と呼ばれるメソッドがありFileExistsます。こちらのドキュメントを参照し てください。fileNameだけを確認する場合GetFileNamesは、IsolatedStorageのルートにあるファイルのファイル名のリストを提供するメソッドを使用することもできます。ここにドキュメント。

IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication();
if(myIsolatedStorage.FileExists("yourxmlfile.xml))
{
    // do this
}
else
{
    // do that
}

また

IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication();
string[] fileNames = myIsolatedStorage.GetFileNames("*.xml")
foreach (string fileName in fileNames)
{
    if(fileName == "yourxmlfile.xml")
    {
      // do this
    }
    else
    {
      // do that
    }
}

上記のコードが正確に機能することを保証するものではありませんが、これはその方法の一般的な考え方です。

于 2011-10-31T11:01:27.837 に答える