访问文件夹,访问服务批量修改图片名
按钮事件:
button1.Enabled = false; //首先把图片的文件夹中的图片名用一个数组存放 // string load = @"C:\Users\742357\Desktop\照片"; string load = textBox1.Text; string[] fileName = System.IO.Directory.GetFiles(load, "*.JPG", System.IO.SearchOption.AllDirectories); int fileCount = fileName.Length;//图片的个数 List<string> ID = new List<string>(); // string newload = @"C:\Users\742357\Desktop\整理后"; string newload = textBox2.Text; FileInfo fi1 = new FileInfo(load); //fi1.CopyTo(newload); System.IO.Directory.CreateDirectory(newload);//创建新的文件夹 foreach (var item in System.IO.Directory.GetFiles(load, "*.JPG", System.IO.SearchOption.AllDirectories)) { string fileOfName2 = Path.GetFileName(item);//得到每个图片的名称 //string[] id = fileOfName2.Split('.'); // string fileOfName = fileOfName2.Replace(".png", ".jpg"); //ID.Add(id[0]); ID.Add(Path.GetFileNameWithoutExtension(item)); // var file = Path.GetFileNameWithoutExtension(item); // string sql = @"select top 1 1 from t_empinfo where cardid=@id and usable=1"; } link.Linkwebservice lk = new link.Linkwebservice(); DataTable dt = new DataTable("t"); dt = lk.getInfo(ID.ToArray()); n = dt.Rows.Count; progressBar1.Maximum = n; label5.Text = "该目录下共发现" + n + "张.JPG后缀文件"; label5.Visible = true; int i = 0; foreach (var item in System.IO.Directory.GetFiles(load, "*.JPG", System.IO.SearchOption.AllDirectories)) { DataRow[] drs = dt.Select("CardID='" + Path.GetFileNameWithoutExtension(item) + "'"); if(drs != null && drs.Length > 0){ // string temp = drs[0]["EmpName"].ToString()+"_"+drs[0]["EmpNum"].ToString(); File.Copy(load + @"\" + Path.GetFileName(item), newload + @"\" + temp+".JPG",true); i++; label3.Visible = false; progressBar1.Visible = true; progressBar1.Value++; } // var item1 = dt.AsEnumerable().Where(a => a["CardID"].ToString() == Path.GetFileName(item)); // string temp=item1[2] } label5.Text = label5.Text + ",转换完成" + i + "张";
初始化:
private void Init() { label5.Visible = false; progressBar1.Visible = false; label3.Visible = false; }
效果图: