C# 批量複製檔案

來源:互聯網
上載者:User

前言
    項目之外,公司要求把客戶上傳的xls資料檔案按條件拷貝一份出來,可是這些上傳的檔案都已經重新命過名,不過還好有上傳的記錄,包括檔案新命名的規則.於是只需要寫一個程式來批量獲得這些被重新命過名的檔案然後拷貝出來就行了.

正題
    我是直接建立的一個aspx並在後台代碼裡寫的,帖cs代碼:using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Data.SqlClient;

public partial class page_FilesAsEasy : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        ArrayList arr = new ArrayList();
        SqlConnection conn = new SqlConnection();
        conn.ConnectionString = @"Data Source=.\;Initial Catalog=test;User ID=test;Password=test";
        conn.Open();
        string sqlcmd;
        sqlcmd = @"select * from test";
        SqlCommand cmd = new SqlCommand(sqlcmd, conn);
        SqlDataReader sdr = cmd.ExecuteReader();
        while (sdr.Read())
        {
            FileVO vo = new FileVO();
            vo.Client_Name = sdr["clientname"].ToString();
            vo.Client_ID = sdr["linkshop"].ToString();
            vo.Category = sdr["category"].ToString();
            vo.Filename = sdr["filename"].ToString();
            arr.Add(vo);
        }
        sdr.Close();
        conn.Dispose();

        Response.Write("開始拷貝檔案..<br/><br/>");
        Response.Flush();

        foreach (object var in arr)
        {
            try
            {
                FileVO item = (FileVO)var;
                //這是經過重新命名的檔案的path
                string from = @"E:\files\" + item.Client_ID.Trim() + "_" + item.Category.Trim() + "_" + item.Filename.Trim() + ".xls";
                string Category = string.Empty;
                switch (item.Category)
                {
                    case "1":
                        Category = "出售";
                        break;
                    case "2":
                        Category = "出租";
                        break;
                    default:
                        Category = "購入";
                        break;
                }
                //重新命名
                string to = @"F:\xlsdata\" + item.Client_Name.Trim() + "_" + Category.Trim() + ".xls";
                //拷貝檔案
                File.Copy(from, to, true);
                //設定檔案屬性
                File.SetAttributes(to, FileAttributes.Normal);
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message + "<br/>");
                return;
            }
        }
        Response.Write("<br/>拷貝檔案結束..");
    }
    class FileVO
    {
        private string _Client_ID;
        /// <summary>
        /// 客戶代號
        /// </summary>
        public string Client_ID
        {
            get { return _Client_ID; }
            set { _Client_ID = value; }
        }

        private string _Category;
        /// <summary>
        /// 業務類型
        /// </summary>
        public string Category
        {
            get { return _Category; }
            set { _Category = value; }
        }

        private string _Filename;
        /// <summary>
        /// 檔案名稱
        /// </summary>
        public string Filename
        {
            get { return _Filename; }
            set { _Filename = value; }
        }

        private string _Client_Name;
        /// <summary>
        /// 客戶名稱
        /// </summary>
        public string Client_Name
        {
            get { return _Client_Name; }
            set { _Client_Name = value; }
        }
    }
}

注意:
    這裡最關鍵是以下兩句代碼://拷貝檔案
File.Copy(from, to, true);
//設定檔案屬性
File.SetAttributes(to, FileAttributes.Normal);

特別是第二句,我花了許多時間才找到的,如果你不加這句話,那會報以下錯誤:The process cannot access the file 'F:\xlsdata\xxx_xxx.xls' because it is being used by another process.

OK!!批量重新命名複製成功!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.