博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C# 批量复制文件
阅读量:6992 次
发布时间:2019-06-27

本文共 2878 字,大约阅读时间需要 9 分钟。

前言
    项目之外,公司要求把客户上传的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!!批量重命名复制成功!

转载:http://www.cnblogs.com/over140/archive/2007/12/07/986326.html

你可能感兴趣的文章
jQuery过滤选择器:not()方法
查看>>
pietty自动登录
查看>>
输入n,求一个n×n矩阵,规定矩阵沿45度递增,形成zigzag数组
查看>>
FREEBSD上vsftp+MYSQL实现虚拟用户验证
查看>>
php.ini配置解释
查看>>
cordova与ios native code交互的原理
查看>>
vue tab组件
查看>>
使用supervisord管理swoole服务踩过的一个坑
查看>>
12种JavaScript MVC框架之比较
查看>>
开始学习 Backbone
查看>>
ant 执行 scp 命令
查看>>
ubuntu在/usr下面添加删除文件
查看>>
android 度量单位 dp sp px DPI
查看>>
Oracle 中的 同义词(synonym)
查看>>
使用nginx-负载均衡基本配置
查看>>
基于JQuery框架的AJAX实例代码
查看>>
Common Lisp String 常用函数用法
查看>>
利用Mytatis Generator插件生成Model的列表查询方法
查看>>
Delphi ICS 多线程下载
查看>>
lucene4.0与IKAnalyzer2012_u6的冲突
查看>>