I have this code but it gives IOException. What is the problem with "File.Move(files[i].FullName, newFileWithPath);" line?
==================================================
private void button1_Click(object sender, System.EventArgs e)
{
DialogResult result = folderBrowserDialog1.ShowDialog();
if (result == DialogResult.OK)
{
string pathname = @folderBrowserDialog1.SelectedPath;
FileInfo[] files = new DirectoryInfo(pathname).GetFiles("*.JPG");
for (int i = 0; i < files.Length; i++)
{
string newFile = String.Format("{0}{1}.{2}", "A", (i+1), "JPG");
string fullNewPath = files[i].DirectoryName;
string newFileWithPath = Path.Combine(fullNewPath, newFile);
File.Move(files[i].FullName, newFileWithPath);
}
}
}
==================================================
Loading

Hemant SrivastavaRank #581 · 4.2K reputationPosted Sep 27, 2013, 4:17 PM EDT
I think, you may check whether file is locked or not, if not only then rename using following way:
http://stackoverflow.com/questions/626058/file-used-by-another-process
Hemant SrivastavaRank #581 · 4.2K reputationPosted Sep 27, 2013, 3:50 PM EDT
Wondering...are you getting proper value of 'newFileWithPath' till line
string newFileWithPath = Path.Combine(fullNewPath, newFile);
(pls check the value by setting breakpoint... coz most of the times in IO errors are due to wrong path... may be missing \ slash)
Ali G51 reputationPosted Sep 27, 2013, 3:55 PM EDT
Ali G51 reputationPosted Sep 27, 2013, 4:19 PM EDT
Ali G51 reputationPosted Sep 29, 2013, 2:28 AM EDT