代码改变世界

服务器下载文件

2011-03-28 10:43  三皮开发时  阅读(280)  评论(0编辑  收藏  举报
以下方法是从pURL路径,下载pURL/pFileName文件至本地pLocalFolderPath
        /// <summary>
        
/// 下载文件至本地文件夹
        
/// </summary>
        
/// <param name="pURL">下载路径</param>
        
/// <param name="pFileName">文件名</param>
        
/// <param name="pLocalFolderPath">本地文件夹</param>
        
/// <returns>本地文件的完整路径</returns>
        public static string DownloadFile(string pURL, string pFileName, string pLocalFolderPath)
        {
            Uri address 
= new Uri(pURL + pFileName);
            
string fileFullName = Path.Combine(pLocalFolderPath, pFileName);
            
try
            {
                
//如果需要下载的文件包括文件夹名,则检测文件夹是否存在
                if (pFileName.IndexOf('/'> 0)
                {
                    
string folderName = fileFullName.Substring(0, fileFullName.LastIndexOf('/'));
                    
if (!Directory.Exists(folderName))
                    {
                        Directory.CreateDirectory(folderName);
                    }
                }

                WebRequest request 
= WebRequest.Create(address);
                
//perform the GET request
                using (WebResponse response = request.GetResponse())
                {
                    
//get stream containing received data
                    using (Stream receiveStream = response.GetResponseStream())
                    {
                        
//去掉目标文件的只读属性
                        FileInfo fileInfo = new FileInfo(fileFullName);
                        
if (File.Exists(fileFullName))
                        {
                            fileInfo.Attributes 
= fileInfo.Attributes & ~FileAttributes.ReadOnly;
                        }
                        
//open filestream for the output file
                        using (FileStream fs = fileInfo.Open(FileMode.Create, FileAccess.Write))
                        {
                            
//copy until all data is read
                            byte[] buffer = new byte[1024];
                            
int bytesRead = receiveStream.Read(buffer, 0, buffer.Length);
                            
while (bytesRead > 0)
                            {
                                fs.Write(buffer, 
0, bytesRead);
                                bytesRead 
= receiveStream.Read(buffer, 0, buffer.Length);
                            }
                        }
                    }
                }
            }
            
catch (Exception ex)
            {
                MessageBox.Show(String.Format(
"从{0}下载文件时出错,错误消息:{1}", address.ToString(), ex.Message), "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
                
throw ex;
            }
            
return fileFullName;
        }

 

以下是调用方法

 

 

FileUtility.DownloadFile(LocalConfig.UpdateURL, "Update.xml", Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments));

 

LocalConfig.UpdateURL :http://10.10.21.182/TerminalService/AutoUpdate/eFormPC/

Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments): C:\Documents and Settings\Administrator\My Documents  (根据登录的当前用户,此处是Administrator)

 

即:从http://10.10.21.182/TerminalService/AutoUpdate/eFormPC/下载http://10.10.21.182/TerminalService/AutoUpdate/eFormPC/Update.xml至本地C:\Documents and Settings\Administrator\My Documents下。

 

【注】:服务器的同步服务需要设置属性,执行脚本设置为纯脚本