using System;

using System.Collections.Generic;

using System.IO;

using System.Linq;

using System.Net;

using System.Text;

 

 

class FileDown

{

 

    /// <summary>

    /// 파일이름을 저장할 있는 이름으로 바꿔주는 메소드

    /// </summary>

    /// <param name="fName"></param>

    /// <returns></returns>

    public static string ReplaceFileName(string fName)

    {

        Dictionary<string, string> dicSpStr = new Dictionary<string, string>

        {

            {"\\", "_"}

            ,{"/", "_"}

            ,{":", "_"}

            ,{"*", "_"}

            ,{"?", "_"}

            ,{"\"", "_"}

            ,{"<", "["}

            ,{">", "]"}

            ,{"|", "_"}

            ,{" ", "_"}

        };

 

        foreach (KeyValuePair<string, string> spStr in dicSpStr)

        {

            fName = fName.Replace(spStr.Key, spStr.Value);

        }

 

        return fName;

    }

 

    /// <summary>

    /// 실제 파일 다운로드(URL주소, 저장할 파일명)

    /// </summary>

    /// <param name="fileUrl"></param>

    /// <param name="fileName"></param>

    /// <param name="Extension"></param>

    public static string FileDownLoad(string fileUrl, string savePath)

    {

        string rtn = string.Empty;

        try

        {

            FileInfo fi = new FileInfo(savePath);

 

            DirectoryInfo di = new DirectoryInfo(fi.Directory.ToString());

            if (!di.Exists)

            {

                di.Create();

            }

 

            WebClient webclient = new WebClient();

            Uri uri = new Uri(fileUrl);

            webclient.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 2.0.50727; .NET CLR 3.0.04506.590; .NET CLR 3.5.20706; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729;)");

            webclient.Credentials = new NetworkCredential("sitemgr", "S2_.T8UJ");

            webclient.UseDefaultCredentials = true;

            webclient.DownloadFileAsync(uri, savePath);

            //while (webclient.IsBusy) { System.Windows.Forms.Application.DoEvents(); System.Threading.Thread.Sleep(1); };

            while (webclient.IsBusy) { System.Threading.Thread.Sleep(1); };

 

            rtn = "true";

        }

        catch (Exception x)

        {

            rtn = x.Message;

        }

        return rtn;

 

    }

 

}

Posted by motolies
,