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 SrivastavaPosted Sep 27, 2013, 8:17 PM
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
Ali GPosted Sep 29, 2013, 6:28 AM
Ali GPosted Sep 27, 2013, 8:19 PM
Ali GPosted Sep 27, 2013, 7:55 PM
Hemant SrivastavaPosted Sep 27, 2013, 7:50 PM
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)