private bool TryRenameItem(PdfItemInfo item, PdfItemCollection col) { string dir = col.DirectoryPath; string oldFileName = System.IO.Path.Combine(dir, item.OriginalName); try { string newFileName = System.IO.Path.Combine(dir, item.SuggestedName); if (File.Exists(newFileName)) { const int maxTries = 100; int counter = 1; for (; counter < maxTries; counter++) { string ext = System.IO.Path.GetExtension(newFileName); string baseFileName = System.IO.Path.GetFileNameWithoutExtension(newFileName) + String.Format(" ({0})", counter) + ext; string dirName = System.IO.Path.GetDirectoryName(newFileName); newFileName = System.IO.Path.Combine(dirName, baseFileName); if (!File.Exists(newFileName)) { break; } } if (counter == maxTries) { item.Message = "Could not rename file! Reason: files with this name and its numbered variants already exist."; return(false); } } if (oldFileName != newFileName) { try { File.Move(oldFileName, newFileName); return(true); } catch (Exception ex) { item.Message = "Could not rename file! Reason: " + ex.Message; } } } catch (Exception ex) { item.Message = "Could not rename file! Reason: " + ex.Message; } return(false); }
private bool TryRenameItem(PdfItemInfo item, PdfItemCollection col) { string dir = col.DirectoryPath; string oldFileName = System.IO.Path.Combine(dir, item.OriginalName); try { string newFileName = System.IO.Path.Combine(dir, item.SuggestedName); if (File.Exists(newFileName)) { const int maxTries = 100; int counter = 1; for (; counter < maxTries; counter++) { string ext = System.IO.Path.GetExtension(newFileName); string baseFileName = System.IO.Path.GetFileNameWithoutExtension(newFileName) + String.Format(" ({0})", counter) + ext; string dirName = System.IO.Path.GetDirectoryName(newFileName); newFileName = System.IO.Path.Combine(dirName, baseFileName); if (!File.Exists(newFileName)) break; } if (counter == maxTries) { item.Message = "Could not rename file! Reason: files with this name and its numbered variants already exist."; return false; } } if (oldFileName != newFileName) { try { File.Move(oldFileName, newFileName); return true; } catch (Exception ex) { item.Message = "Could not rename file! Reason: " + ex.Message; } } } catch (Exception ex) { item.Message = "Could not rename file! Reason: " + ex.Message; } return false; }