複製文件到指定的文件夾

using System;
using System.Windows.Forms;
using System.IO;

namespace Common
{
    public class FileHelper
    {
        /// <summary>
        /// 複製文件到指定路徑
        /// </summary>
        /// <param name="sourcePath">原文件的路徑</param>
        /// <returns></returns>
        public static bool CopyFileTo(string sourcePath)
        {
            //文件夾選擇框
            FolderBrowserDialog folderBrowser = new FolderBrowserDialog();
            folderBrowser.Description = "請選擇保存路徑";

            if (folderBrowser.ShowDialog() == DialogResult.OK)
            {
                string destinationPath = Path.Combine(folderBrowser.SelectedPath,
                                                              Path.GetFileName(sourcePath));
                try
                {
                    File.Copy(sourcePath, destinationPath);
                    return true;
                }
                catch (Exception)
                {
                    return false;
                }    
            }

            return false;
        }
    }
}


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章