2011-09-16 53 views
0

我有開始應用此代碼下載尺寸文件阻止執行的

client.DownloadFileAsync(new Uri("http://mysite/myapplication.exe"), tempPath + "\\" + sDownloadFileName); 

        try 
        { 
        string tempPath = System.IO.Path.GetTempPath(); 
        Process.Start(tempPath + "myapplication.exe");     
        Application.Exit(); 
        } 
        catch 
        { 
        MessageBox.Show"Error, the file is corrupt"); 
        } 

之後,但如果文件(HTTP://mysite/myapplication.exe)不存在.. 一個文件創建的相同1KB,啓動並返回錯誤=( 可能塊的執行文件,如果是小的10 MB?

    try 
        { 
        string tempPath = System.IO.Path.GetTempPath();       
        if (tempPath + "myapplication.exe" == -10 MB) 
        { 
        MessageBox.Show"Error, the file is corrupt"); 
        } 
        else if 
        { 
        Process.Start(tempPath + "myapplication.exe");  
        } 
        } 

回答

1

使用FileInfo下載後檢查一下大小。

try 
{ 
    string tempPath = System.IO.Path.GetTempPath(); 
    FileInfo fileInfo = new FileInfo(downloadFilePath); 
    if (fileInfo.Length < 10*1000*1000) 
    { 
     MessageBox.Show"Error, the file is corrupt"); 
    } 
    else if 
    { 
     Process.Start(tempPath + "myapplication.exe");  
    } 
}