2011-10-31 22 views
1

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

私は、ファイルが条件で存在するかどうかを確認する方法がわからない:(

答えて

5

IsolatedStorageFileクラスメソッドはFileExistsと呼ばれていdocumentation here の場合を参照してください。 fileNameだけをチェックする場合は、GetFileNamesメソッドを使用して、IsolatedStorageのルートにあるファイルのファイル名のリストを取得します。Documentation here.

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 
    } 
} 

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

+0

でも、私はこの状態で何をしていますか?私は理解していない:(if(getfilename.xml_file = true)???????? – jpmd

+0

また、foreachが文字列配列で動作するかどうかわからない。通常のforループを試してみよう。 – abhinav

+0

ありがとう;魅力;) – jpmd

関連する問題