使用C#重命名文件名 [英] Renaming file name using C#
本文介绍了使用C#重命名文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的任务是获取mdf文件,并且必须以编程方式将该文件附加到数据库,并且当另一个文件出现时,我必须分离该文件并对其进行备份并附加一个新文件.我的问题是,通过备份,我想按备份顺序(如backup1,backup2 .....等)提供该文件的名称.我需要帮助,如何以编程方式以递增顺序命名.
I have a task in that i get a mdf file, and i have to attach programatically that file to database and when another file comes i have to detach that and take backup of that and attach new one. My problem is, by taking backup i want to give names of that file in incremental order like backup1, backup2.....and so on. I need help how can i give names in incremental order programatically.
推荐答案
^ ]
用于进行备份(即复制到新名称)
File.Copy [
File.Move[^]
For taking the backup (i.e. copying to a new name)
File.Copy[^]
使用 ^ ]和 Directory.EnumerateFiles [ ^ ]查找带有"backup *"的文件夹中的所有文件.
使用LINQ排序并获取MAX文件计数名称.喜欢
Use Directory Class[^] and Directory.EnumerateFiles[^] to find all files in the folder with "backup*".
Use LINQ to sort and get the MAX file count name. Like
var file = (from file in Directory.EnumerateFiles(@"c:\archives1\library\", "backup*.txt")
order by file
select file).LastOrDefault();
然后使用文件类 [
And then use File class[^] file operation.
我将在应用程序设置中保留一个int变量,该变量将在每次需要备份时使用并递增.这样的事情(其中Properties.Settings.Default.fileCount
是存储在应用程序设置"中的变量
I would keep an int variable in the Application Settings, which I would use and increment every time I needed to backup. Something like this (whereProperties.Settings.Default.fileCount
is the variable stored in Application Settings
int currentFileNumber = Properties.Settings.Default.fileCount;
string newFileName = String.Format("YourFileName{0}.mdb", currentFileNumber + 1);
File.Copy(string.Format("YourFileName{0}.mdb", currentFileNumber), string.Format("YourFileName{0}.mdb", currentFileNumber + 1));
Properties.Settings.Default.fileCount = currentFileNumber + 1;
Properties.Settings.Default.Save();
希望对您有帮助
Hope this helps
这篇关于使用C#重命名文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文