2011-10-30 1 views
0

) 인터넷에서 메모리 폰으로 xml 파일을 다운로드합니다. 인터넷 연결을 통해 다운로드하고 메시지를 보낼 수 있는지 확인하고 싶습니다. 그리고 만약 내가 xml 파일이 이미 메모리에 존재하는지 확인하고 싶지 않다면, appliccation은 다운로드를하지 않습니다.인터넷 연결이 가능한지 확인하십시오

나는 다음과 같은 코드가 있습니다

public MainPage() 
    { 
     InitializeComponent(); 
     WebClient downloader = new WebClient(); 
     Uri xmlUri = new Uri("http://dl.dropbox.com/file_xml.xml", UriKind.Absolute); 
     downloader.DownloadStringCompleted += new DownloadStringCompletedEventHandler(Downloaded); 
     downloader.DownloadStringAsync(xmlUri); 




    } 

    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(); 


       using (StreamWriter writeFile = new StreamWriter(new IsolatedStorageFileStream("xml_file.xml", FileMode.Create, FileAccess.Write, myIsolatedStorage))) 
       { 
        string xml_file = e.Result.ToString(); 
        writeFile.WriteLine(xml_file); 
        writeFile.Close(); 
       } 
      } 
     } 
    } 

답변

3

NetworkInterface.GetIsNetworkAvailable() 메소드는 당신이 말할 수 있습니다 :

using System.Net.NetworkInformation; 
... 
if (NetworkInterface.GetIsNetworkAvailable()) 
{ 
    // do network-bound stuff 
} 
else 
{ 
    // notify the user that there is no network connection 
} 
+0

) (GetIsNetworkAvailable'에 계획 결과를 명명'미친 어떤 종류의!? – Eric

+0

그 결과는 응답에 30 초 (정확하게 기억한다면) 걸리기 때문에 호출됩니다. 그래서 그 순간. – invalidusername

+0

감사합니다;)이 작동;) – jpmd