C#实现快速重命名JPEG文件

工作的原因,要对大量的图片(主要是JPEG文件)进行重命名的处理。本人用C#编写了一个小程序,在这里和大家分享一下。也算是总结一下吧!

一、从Excel文件中读取数据

     Excel文件中主要是一个学生的数据库,包含学生的姓名、性别、学号等信息,在本程序中,只需要将Excel文件中的序号和学号读入到程序中即可。代码如下:

     

ContractedBlock.gif ExpandedBlockStart.gif Code
        private void ConstructExcelDB(string fileName,int length)
        {
            
string connStr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileName + ";Extended Properties=Excel 8.0";
            OleDbConnection conn 
= new OleDbConnection(connStr);
            OleDbCommand comm 
= new OleDbCommand();
            comm.CommandText 
= "Select [序号],[学号] from [AllStudents$]";
            comm.Connection 
= conn;

            OleDbDataReader reader;

            
try
            {
                conn.Open();
                reader 
= comm.ExecuteReader();
                
while (reader.Read())
                {
                    
if (!excels.ContainsKey(FormatStr((reader["序号"].ToString()).Trim(),length)))
                    {
                        excels.Add(FormatStr((reader[
"序号"].ToString()).Trim(),length), (reader["学号"].ToString()).Trim());
                    }
                }
            }
            
catch (Exception e)
            {
                MessageBox.Show(e.Message.ToString(), 
"Error");
            }            
        }

        
private string FormatStr(string str,int length)
        {
            
string temp = string.Empty;
            
for (int i = 0; i < length - str.Length; i++)
            {
                temp 
+= "0";
            }

            
return temp + str;
        }

 

这里出现一个问题,就是从Excel文件中的Sheet1工作薄中的“序号”字段中读出的数据不能正确读取,如对于“0001”,它读取的结果为“1”,所以我使用了FormatStr()方法将格式化该字段中的数据。excels是一个私有成员,其声明代码为:

 

ContractedBlock.gif ExpandedBlockStart.gif Code
 private Dictionary<stringstring> excels = new Dictionary<stringstring>();

 

二、执行重命名操作

     当单击“开始转换”按钮后,应用程序首先遍历你选择的目录,然后将JPEG文件的名字(开始时是以序号来命名的)转换为相对应的学号来命名。代码如下:

 

ContractedBlock.gif ExpandedBlockStart.gif Code
        private void RenameAllJPEGFiles(string path)
        {
            DirectoryInfo dir 
= new DirectoryInfo(path);
            DirectoryInfo[] dirs 
= dir.GetDirectories();

            FileInfo[] files 
= dir.GetFiles();

            
foreach (FileInfo fi in files)
            {
                
if (fi.Extension == ".JPG")
                {
                    Computer myCom 
= new Computer();
                    
if (excels.ContainsKey(GetName(fi.Name)))
                    {
                        
string str = excels[GetName(fi.Name)] + fi.Extension;
                        myCom.FileSystem.RenameFile(fi.FullName, str);
                        tbxDetails.AppendText(Environment.NewLine 
+ fi.Name + "  ----->  " + str);
                        tbxDetails.ScrollToCaret();

                        jpegCount
++;
                        
                    }
                }
            }

            
foreach (DirectoryInfo di in dirs)
            {
                RenameAllJPEGFiles(di.FullName);
            }
        }

 

这样,所选目录中的所有JPEG文件就重命名成功了。

程序界面如下:

转载于:https://www.cnblogs.com/WHCSHARPER/archive/2008/10/23/1317974.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值