根据文件夹中的文件名创建文件? [英] Creating Files based on Files name in a folder ?
问题描述
您好。
我的应用程序在文件夹中创建文件,具有基于数字的文件名(例如txt文件:1.txt,2.txt,... )
My app creates files in a folder, with numerical-based file names (for e.g. txt files : 1.txt , 2.txt , ...)
因此,如果有3个文件,则第四个文件的名称应为4。这很简单:
So if there are 3 files, the forth file's name should be 4 . It is quite simple:
Directory.GetFiles(Application.StartupPath & "\_customers\", "*.cinfo").Count + 1
但是当在同一文件夹中的其他文件中删除文件时,会出现问题。如果有5个文件并且删除了第四个文件,则文件为:1.txt,2.txt,3.txt,5.txt。现在使用上面的代码,app会创建 名为
5的新文件(已存在5)。应用程序应该遵循创建新文件的数字模式。如何做到这一点?
But when it comes to deleting a file among other files in the same folder , there will be problem. If there are 5 files and the forth is deleted , files are : 1.txt, 2.txt, 3.txt, 5.txt . Now with the code above, app creates new file with the name 5 (5 already exists). App should follow the numerical pattern in creating new files.How to do the trick ?
推荐答案
我的应用程序在文件夹中创建文件,使用基于数字的文件名(例如txt文件:1.txt,2.txt,.. 。)
My app creates files in a folder, with numerical-based file names (for e.g. txt files : 1.txt , 2.txt , ...)
因此,如果有3个文件,则第四个文件的名称应为4。这很简单:
So if there are 3 files, the forth file's name should be 4 . It is quite simple:
但是当在同一文件夹中的其他文件中删除文件时,会出现问题。如果有5个文件并且删除了第四个文件,则文件为:1.txt,2.txt,3.txt,5.txt。现在使用上面的代码,app会创建 名为
5的新文件(已存在5)。应用程序应该在创建新文件时遵循数字模式。
But when it comes to deleting a file among other files in the same folder , there will be problem. If there are 5 files and the forth is deleted , files are : 1.txt, 2.txt, 3.txt, 5.txt . Now with the code above, app creates new file with the name 5 (5 already exists). App should follow the numerical pattern in creating new files.
假设你的意思是在这种情况下你想要一个新文件使用6.txt,这是一个相当简陋的编程任务。
获取目录中所有文件名的列表。
确定列表中的最高数字文件名。$
开始为一个更大的新文件编号。
- Wayne
Assuming you mean that you want a new file in this case to use 6.txt,
it sounds like a rather rudimentary programming task.
Get a list of all filenames in the directory.
Identify the highest numerical filename in the list.
Start the numbering for new files at one greater.
- Wayne
这篇关于根据文件夹中的文件名创建文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!