2011-09-16 46 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");  
    } 
}