2
Answers

After Reaching Exif Data I can't Rename Them

Ali G

Ali G

11y
906
1
Hen I reach the jpeg's date and time. I can't change that file's name. It gives exception. How can I solve this problem. Files are behaving like "in use" and I can't change their names.
My codes are here:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging; //YENI
using System.IO; //YENI

namespace PhotoRenamer2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            DialogResult result = folderBrowserDialog1.ShowDialog();
            string pathname = @folderBrowserDialog1.SelectedPath;
            label2.Text = pathname;
            FileInfo[] files = new DirectoryInfo(pathname).GetFiles("*.JPG");
            for (int i = 0; i < files.Length; i++)
            {
                string DateAndTimePictureTaken = string.Empty;
                Image image = new Bitmap(@files[i].FullName);
                foreach (PropertyItem propItem in image.PropertyItems)
                {
                    if (propItem.Id == 0x0132)
                    {
                        DateAndTimePictureTaken = (new System.Text.ASCIIEncoding()).GetString(propItem.Value);
                        string Year = DateAndTimePictureTaken.Substring(0, 4);
                        label3.Text = Year;
                        string newFile = String.Format("{0}{1}.{2}", "a", (i + 1), "JPG");
                        string fullNewPath = files[i].DirectoryName;
                        string newFileWithPath = Path.Combine(fullNewPath, newFile);
                        label2.Text = files[i].FullName;
                        label3.Text = newFileWithPath;
                        File.Move(files[i].FullName, newFileWithPath);
                        break;
                    }
                }
            }
        }

    }
}

Answers (2)