2011-10-31 5 views
1

xml 파일을 인터넷에서 메모리 폰으로 다운로드합니다. 인터넷 연결을 통해 다운로드하고 메시지를 보낼 수 있는지 확인하고 싶습니다. 그리고 만약 내가 xml 파일이 이미 메모리에 존재하는지 확인하고 싶지 않다면, appliccation은 다운로드를하지 않습니다.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 문자열 배열을 사용할 수 있는지 모르겠습니다. 일반적인 루프를 사용해보십시오. – abhinav

+0

감사합니다;) 작동합니다. 매력;) – jpmd

관련 문제