Sub file_createAA()
Dim filepath As String = System.IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.MyDocuments, "calculator\addition\attack
Sub file_createAA()
Dim filepath As String = System.IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.MyDocuments, "calculator\addition\attack\1.txt")
If Not System.IO.File.Exists(filepath) Then
System.IO.File.Create(filepath).Dispose()
End If
End Sub
This code is repeated 64 times, each time creating a new file. The code below, also repeated 64 times with each slightly varied, also makes the folder paths required. Is there any way to slim this down as each one is rather long.
此代码重复64次,每次创建一个新文件。下面的代码也重复64次,每次略有变化,也使得文件夹路径成为必需。有没有办法减少这个因为每个人都很长。
My.Computer.FileSystem.CreateDirectory(System.IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.MyDocuments, "calculator\addition\attack"))
1 个解决方案
#1
How about this:
这个怎么样:
Sub file_createAA(dir1 As String, dir2 As String)
'' dir1 can be one of these: addition, subtraction, multiplication, division
'' dir2 can be on of these: attack, elimination
Dim index As Integer = 1
Dim filepath As String = System.IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.MyDocuments, String.Format("calculator\{0}\{1}\", dir1, dir2))
Try
Do Until index = 5
If Not System.IO.File.Exists(filepath & index & ".txt") Then
System.IO.Directory.CreateDirectory(filepath)
System.IO.File.Create(filepath & index & ".txt").Dispose()
End If
index += 1
Loop
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
When you call your sub, you would do something like this:
当你打电话给你的sub时,你会做这样的事情:
file_createAA("addition", "attack")
file_createAA("subtraction", "attack")
file_createAA("multiplication", "attack")
file_createAA("division", "attack")
file_createAA("addition", "elimination")
file_createAA("subtraction", "elimination")
file_createAA("multiplication", "elimination")
file_createAA("division", "elimination")
This then actually means that if you ever wanted to just create 1 file instead of them all, you could call the sub once with the relevant parameters. I think this is what you were asking for :P Oh and also, it now creates the directories, sub directories and files if they don't exist. If this isn't what you wanted just ask and I'll be happy to help. :)
这实际上意味着如果您只想创建1个文件而不是全部,则可以使用相关参数调用sub一次。我认为这就是你要求的:P哦,而且,它现在创建目录,子目录和文件,如果它们不存在。如果这不是您想要的,那么我会很乐意提供帮助。 :)
.txt")
If Not System.IO.File.Exists(filepath) Then
System.IO.File.Create(filepath).Dispose()
End If
End Sub
Sub file_createAA()
Dim filepath As String = S