简体   繁体   中英

Generate multiple files in a loop using visual basic

I am new to Visual Basic.

I wonder if I wanna generate multiple files in a while loop. It seems that the FILESTREAM cannot be reused even I set it into nothing at the end of loop.

Dim fs as FILESTREAM = nothing
Dim i as integer = 0
Dim path as string = "c:\users\...\Desktop\"
Dim name as string = nothing
while i<10
name = path + i.tostring

fs=File.Create(name)

i+=1
fs=nothing

end while

Thank you very much!

It's not clear if you're using the FileStream just to create a blank file or if you're planning on performing other operations against the new files. If you're just trying to create empty files and manipulate them later, after creation, then this should work:

Sub Main()
    Dim i As Integer = 0
    ' You can use properties of My.Computer to find the current user's Desktop
    Dim path As String = My.Computer.FileSystem.SpecialDirectories.Desktop
    Dim fileName As String = String.Empty

    While i < 10
        fileName = path & "\" & i.ToString
        System.IO.File.Create(fileName)
        i += 1
    End While
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM