In the .NET Framework 4.0, I use the FileInfo.MoveTo() method, which takes only one argument
Just for moving files my method is as follows
private void Move(string sourceDirName, string destDirName) { DirectoryInfo dir = new DirectoryInfo(sourceDirName); FileInfo[] files = null; files = dir.GetFiles(); foreach (FileInfo file in files) { string temppath = Path.Combine(destDirName, file.Name); file.MoveTo(temppath); } }
for renaming files my method is as follows
private void Rename(string folderPath) { int fileCount = 0; DirectoryInfo dir = new DirectoryInfo(folderPath); files = dir.GetFiles(); foreach (FileInfo file in files) { fileCount += 1; string newFileName = fileCount.ToString() + file.Name; string temppath = Path.Combine(folderPath, newFileName); file.MoveTo(temppath); } }
AS, which you can see to rename the file, its syntax is almost the same as for Move it, you just need to change the filename before using the MoveTo() method.
user275683 Apr 10 '14 at 11:58
source share