Monday, September 22, 2008

Fair And Lovely Hindi

VB.Net: Creating folders

you know the problem? A program to write a file in a directory move or copy, but you are not sure that the folder exists?

For such cases I had in the past, a routine that creates using a recursion nonexistent folder, after all, is already the parent Folders are missing.

The old solution worked well, not only liked me. A recursive function for such a simple purpose? Until I remembered out of the blue a better solution. The idea: split ()

Public Shared Sub folder check (ByVal folder path As String) Dim
asOrdner () As String
; sPfad As String Dim = ""
asOrdner = Split (Folder path "\\" )
    For Each sOrdner As String In asOrdner
    sPfad &= sOrdner & "\"
     If Not Directory.Exists(sPfad) Then Directory.CreateDirectory(sPfad)
    Next
  End Sub
Code-Beispiel 1

Das Ding funktionierte auf Anhieb gut (was immer verdächtig ist); allerdings kann CreateDirectory einen Ausnahmefehler . Generate Therefore, it is better to consider this.

Public Shared Function OrdnerChecked (ByVal folder path As String) As Boolean
Dim asOrdner () As String
Dim sPfad As String = ""
Dim bReturn Boolean = True
asOrdner = Split (Folder path "\\" )
Try
For Each sOrdner As String In asOrdner
    sPfad &= sOrdner & "\"
     If Not Directory.Exists(sPfad) Then _
     Directory.CreateDirectory(sPfad)
    Next
   Catch ex As Exception
   bReturn = False
   End Try Return
bReturn
End Function
Code Example 2

Now, with an If-Then-Else structure examined the existence of a folder, create the appropriate folder and file are stored. If the plant can not possibly be in the program itself responds to the problem (dialogue, etc.)

0 comments:

Post a Comment