C# Snippets


File_AttributesDisplay

Public Shared Sub AttributesDisplay(ByVal sFolderPath As String, _
Optional ByVal sFileName As String = "", _
Optional ByVal sExtension As String = "")

Dim objfileinfo As New System.IO.FileInfo(sFolderPath & sFileName & sExtension)
Dim sfileattributes As String

Try
If gbEND_GENERAL = True Then Exit Sub

If objfileinfo.Attributes = IO.FileAttributes.Archive Then
sfileattributes += "Archive,"
End If
If objfileinfo.Attributes = IO.FileAttributes.Compressed Then
sfileattributes += "Compressed,"
End If
If objfileinfo.Attributes = IO.FileAttributes.Device Then
sfileattributes += "Device,"
End If
If objfileinfo.Attributes = IO.FileAttributes.Directory Then
sfileattributes += "Directory,"
End If
If objfileinfo.Attributes = IO.FileAttributes.Encrypted Then
sfileattributes += "Encrypted,"
End If
If objfileinfo.Attributes = IO.FileAttributes.Hidden Then
sfileattributes += "Hidden,"
End If
If objfileinfo.Attributes = IO.FileAttributes.Normal Then
sfileattributes += "Normal,"
End If
If objfileinfo.Attributes = IO.FileAttributes.NotContentIndexed Then
sfileattributes += "NotContentIndexed,"
End If
If objfileinfo.Attributes = IO.FileAttributes.Offline Then
sfileattributes += "Offline,"
End If
If objfileinfo.Attributes = IO.FileAttributes.ReadOnly Then
sfileattributes += "ReadOnly,"
End If
If objfileinfo.Attributes = IO.FileAttributes.ReparsePoint Then
sfileattributes += "ReparsePoint,"
End If
If objfileinfo.Attributes = IO.FileAttributes.SparseFile Then
sfileattributes += "SparseFile,"
End If
If objfileinfo.Attributes = IO.FileAttributes.System Then
sfileattributes += "System,"
End If
If objfileinfo.Attributes = IO.FileAttributes.Temporary Then
sfileattributes += "Temporary,"
End If

Call System.Windows.Forms.MessageBox.Show(sfileattributes, _
gsDIALOG_PREFIX_GENERAL & "File Attributes", _
Windows.Forms.MessageBoxButtons.OK, _
Windows.Forms.MessageBoxIcon.Information)

Catch objCOMException As System.Runtime.InteropServices.COMException
mobjCOMException = objCOMException
Catch objException As Exception
mobjException = objException

Finally
If gbDEBUG_GENERAL = True Or _
((Not mobjCOMException Is Nothing) Or (Not mobjException Is Nothing)) Then

Call clsError.Handle("AttributesDisplay", msCLASSNAME, _
"display all the attributes for the following file:" & _
gsCRLF & "'" & sFolderPath & sFileName & sExtension & "'.", _
mobjCOMException, mobjException)
End If
End Try
End Sub

File_Browse

Displays the dialog box to allow the user to browse to a file.
Public Function File_Browse(ByVal sFilter As String, _
ByVal iFilterIndex As Integer, _
ByVal sInitialDirectory As String, _
ByVal sDefaultExtension As String, _
ByVal bAddExtensionIfOmitted As Boolean) As String

Dim objFileDialog As System.Windows.Forms.OpenFileDialog

Try
objFileDialog = New System.Windows.Forms.OpenFileDialog
With objFileDialog
.CheckFileExists = True
.CheckPathExists = True
.ShowReadOnly = True
.Filter = sFilter
.FilterIndex = iFilterIndex
.InitialDirectory = sInitialDirectory
.DefaultExt = sDefaultExtension
.AddExtension = bAddExtensionIfOmitted

If .ShowDialog = System.Windows.Forms.DialogResult.OK Then
Return .FileName
End If
Return ""
End With
Catch ex As Exception
Call modMessages.ErrorMessage(System.Reflection.MethodBase.GetCurrentMethod, ex.Message)
Return ""
End Try
End Function

File_BrowseReturn

Public Shared Sub BrowseReturn(ByVal sNewFolderPath As String, _
ByVal sSearchPattern As String, _
ByVal txtFolderPath As System.Windows.Forms.TextBox, _
ByVal cboFileName As System.Windows.Forms.ComboBox)

Try
If gbEND_GENERAL = True Then Exit Sub

Dim safiles() As String

sNewFolderPath = clsFolder.LineAdd(sNewFolderPath)

txtFolderPath.Text = sNewFolderPath
Call clsFolder.FilesToArraySingle(sNewFolderPath, "", safiles, sSearchPattern, False)

If safiles Is Nothing Then
cboFileName.Items.Clear()
cboFileName.Items.Add("No files")
cboFileName.SelectedIndex = 0
Exit Sub
Else
Call clsArray.RemoveChars("array", safiles, sNewFolderPath.Length, 0)
Call clsArray.ToListComboBox(cboFileName, "", safiles)

Call clsFile.ListComboSelect(cboFileName)
End If

Catch objCOMException As System.Runtime.InteropServices.COMException
gobjCOMException = objCOMException
Catch objException As Exception
gobjException = objException

Finally
If gbDEBUG_GENERAL = True Or _
((IsNothing(gobjCOMException) = False Or IsNothing(gobjException) = False)) Then

Call clsError.Handle("BrowseReturn", "clsFolder", _
"transfer the files in the folder to the array ?", _
gobjCOMException, gobjException)
End If
End Try
End Sub

File_ComboBoxFileSelect

Public Shared Sub ComboBoxFileSelect(ByVal objComboBox As System.Windows.Forms.ComboBox)

Try
If gbEND_GENERAL = True Then Exit Sub

Select Case objComboBox.Items.Count
Case 0
objComboBox.Items.Insert(0, "No files")
objComboBox.SelectedIndex = 0
Case 1
objComboBox.SelectedIndex = 0
Case Else
objComboBox.Items.Insert(0, "(" & objComboBox.Items.Count & " files)")
objComboBox.SelectedIndex = 0
End Select

Catch objCOMException As System.Runtime.InteropServices.COMException
mobjCOMException = objCOMException
Catch objException As Exception
mobjException = objException

Finally
If gbDEBUG_GENERAL = True Or _
((Not mobjCOMException Is Nothing) Or (Not mobjException Is Nothing)) Then

Call clsError.Handle("ComboBoxFileSelect", msCLASSNAME, _
"select the file in the combo box.", _
mobjCOMException, mobjException)
End If
End Try
End Sub

File_ContentsRemoveString

Public Shared Sub ContentsRemoveString(ByVal sRemoveString As String, _
ByVal sFolderPath As String, _
Optional ByVal sFileName As String = "", _
Optional ByVal sExtension As String = "")

Try
If gbEND_GENERAL = True Then Exit Sub

Dim objStreamReader As System.IO.StreamReader
Dim objStreamWriter As System.IO.StreamWriter
Dim sfullpath As String
Dim stextcontents As String

sfullpath = sFolderPath & sFileName & sExtension

If clsFile.Exists(sfullpath) = False Then
Call clszMessagesGeneral.FileDoesNotExistWarning(sfullpath, "")
Exit Sub
End If

objStreamReader = New System.IO.StreamReader(sfullpath)
stextcontents = objStreamReader.ReadToEnd
objStreamReader.Close()

stextcontents = stextcontents.Replace(sRemoveString, "")

objStreamWriter = New System.IO.StreamWriter(sfullpath)
objStreamWriter.WriteLine(stextcontents)
objStreamWriter.Close()

Catch objCOMException As System.Runtime.InteropServices.COMException
mobjCOMException = objCOMException
Catch objException As Exception
mobjException = objException

Finally
If gbDEBUG_GENERAL = True Or _
((Not mobjCOMException Is Nothing) Or (Not mobjException Is Nothing)) Then

Call clsError.Handle("ContentsRemoveString", msCLASSNAME, _
"remove the string '" & sRemoveString & "' from the text file.", _
mobjCOMException, mobjException)
End If
End Try
End Sub

File_Copy

Copies a file from one folder to another.
Public Function File_Copy(ByVal strSourcePath As String, _
ByVal strDestinationPath As String) As Boolean

Try
If My.Settings.USER_ERROR_OCCURRED = True Then Exit Function

If modGeneral.File_Exists(strSourcePath, True) Then
System.IO.File.Copy(strSourcePath, strDestinationPath)
Return True
Else
Return False
End If

Catch ex As System.Exception
Call modMessages.ErrorMessage(System.Reflection.MethodBase.GetCurrentMethod, ex.Message)
End Try
End Function

File_CopyTo

Public Shared Function CopyTo(ByVal sFolderFrom As String, _
ByVal sFileName As String, _
ByVal sFolderTo As String, _
Optional ByVal sExtension As String = "", _
Optional ByVal sNewFileName As String = "", _
Optional ByVal bOverwrite As Boolean = True) _
As Boolean

Try
If gbEND_GENERAL = True Then Exit Function

Dim objfileinfo As New System.IO.FileInfo(sFolderFrom & sFileName & sExtension)

If clsFolder.Exists(sFolderTo) = False Then
clsFolder.Create(sFolderTo)
End If

objfileinfo.CopyTo(sFolderTo & sFileName & sExtension, bOverwrite)

Catch objCOMException As System.Runtime.InteropServices.COMException
mobjCOMException = objCOMException
Catch objException As Exception
mobjException = objException

Finally
If gbDEBUG_GENERAL = True Or _
((Not mobjCOMException Is Nothing) Or (Not mobjException Is Nothing)) Then

Call clsError.Handle("CopyTo", msCLASSNAME, _
"copy the following file:" & _
gsCRLF & "'" & sFolderFrom & sFileName & sExtension & "'" & _
gsCRLF & "to the folder and filename: " & _
"'" & sFolderTo & sFileName & sExtension & "'", _
mobjCOMException, mobjException)
End If
End Try
End Function

File_Delete

Public Shared Sub Delete(ByVal sFolderPath As String, _
Optional ByVal sFileName As String = "", _
Optional ByVal sExtension As String = "")

Try
If gbEND_GENERAL = True Then Exit Sub

Dim objfileinfo As New System.IO.FileInfo(sFolderPath & sFileName & sExtension)

objfileinfo.Delete()

Catch objCOMException As System.Runtime.InteropServices.COMException
mobjCOMException = objCOMException
Catch objException As Exception
mobjException = objException

Finally
If gbDEBUG_GENERAL = True Or _
((Not mobjCOMException Is Nothing) Or (Not mobjException Is Nothing)) Then

Call clsError.Handle("Delete", msCLASSNAME, _
"delete the following file:" & _
gsCRLF & "'" & sFolderPath & sFileName & sExtension & "'.", _
mobjCOMException, mobjException)
End If
End Try
End Sub

File_DialogSave

Public Function File_DialogSave(ByVal sFilter As String, _
ByVal iFilterIndex As Integer, _
Optional ByVal sInitialDirectory As String = "", _
Optional ByVal bAddExtension As Boolean = False, _
Optional ByVal sDefaultExt As String = "", _
Optional ByVal bCheckFileExists As Boolean = False, _
Optional ByVal bCheckPathExists As Boolean = False) As String

Dim objSaveFileDialog As System.Windows.Forms.SaveFileDialog

Try
objSaveFileDialog = New System.Windows.Forms.SaveFileDialog

With objSaveFileDialog
.Filter = sFilter
.InitialDirectory = sInitialDirectory
.CheckFileExists = bCheckFileExists
.CheckPathExists = bCheckPathExists
.AddExtension = bAddExtension
.DefaultExt = sDefaultExt
.FilterIndex = iFilterIndex

If .ShowDialog = System.Windows.Forms.DialogResult.OK Then
Return .FileName
End If
End With
Return ""

Catch ex As System.Exception
Call modMessages.ErrorMessage(System.Reflection.MethodBase.GetCurrentMethod, ex.Message)
Return ""
End Try
End Function

File_Exists

Public Shared Function Exists(ByVal sFolderPath As String, _
Optional ByVal sFileName As String = "", _
Optional ByVal sExtension As String = "", _
Optional ByVal bInformUser As Boolean = False) _
As Boolean

Dim objFileInfo As System.IO.FileInfo

Try
If gbEND_GENERAL = True Then Exit Function

objFileInfo = New System.IO.FileInfo(sFolderPath & sFileName & sExtension)

Exists = objfileinfo.Exists

If Exists = False And bInformUser = True Then
Call clszMessagesGeneral.FileDoesNotExistWarning(sFolderPath, sFileName)
End If

Catch objCOMException As System.Runtime.InteropServices.COMException
mobjCOMException = objCOMException
Catch objException As Exception
mobjException = objException

Finally
If gbDEBUG_GENERAL = True Or _
((Not mobjCOMException Is Nothing) Or (Not mobjException Is Nothing)) Then

Call clsError.Handle("Exists", msCLASSNAME, _
"determine if the following file exists:" & _
gsCRLF & "'" & sFolderPath & sFileName & sExtension & "'.", _
mobjCOMException, mobjException)
End If
End Try
End Function

File_LastWriteTimeGet

Public Shared Function LastWriteTimeGet(ByVal sFolderPath As String, _
Optional ByVal sFileName As String = "", _
Optional ByVal sExtension As String = "") _
As System.DateTime

Try
If gbEND_GENERAL = True Then Exit Function

Dim objfileinfo As New System.IO.FileInfo(sFolderPath & sFileName & sExtension)

LastWriteTimeGet = objfileinfo.LastWriteTime

Catch objCOMException As System.Runtime.InteropServices.COMException
mobjCOMException = objCOMException
Catch objException As Exception
mobjException = objException

Finally
If gbDEBUG_GENERAL = True Or _
((Not mobjCOMException Is Nothing) Or (Not mobjException Is Nothing)) Then

Call clsError.Handle("LastWriteTimeGet", msCLASSNAME, _
"return the 'LastWriteTime' for the following file:" & _
gsCRLF & _
"'" & sFolderPath & sFileName & sExtension & "'.", _
mobjCOMException, mobjException)
End If
End Try
End Function

File_NameFromFullPath

Public Function File_NameFromFullPath(ByVal sFullPath As String) As String

Try
Return System.IO.Path.GetFileName(sFullPath)

Catch ex As System.Exception
Call modMessages.ErrorMessage(System.Reflection.MethodBase.GetCurrentMethod, ex.Message)
Return ""
End Try
End Function

File_ReadAllowed

Public Shared Function ReadAllowed(ByVal sFolderPath As String, _
Optional ByVal sFileName As String = "", _
Optional ByVal sExtension As String = "") _
As Boolean

'Dim objFileStream As System.IO.FileStream
'objFileStream = New System.IO.FileStream(sfullpath, _
' System.IO.FileMode.Open, _
' System.IO.FileAccess.Read, _
' System.IO.FileShare.None)
'ReadAllowed = objFileStream.CanRead

Dim objStreamReader As System.IO.StreamReader
Dim sfullpath As String

Try
ReadAllowed = False

sfullpath = sFolderPath & sFileName & sExtension

objStreamReader = New System.IO.StreamReader(System.IO.File.OpenRead(sfullpath))
ReadAllowed = True

Catch objCOMException As System.Runtime.InteropServices.COMException
Catch objException As Exception

Finally
If ReadAllowed = False Then
Call clszMessagesGeneral.FileBeingUsedByAnotherProcessWarning(sfullpath)
End If
End Try

End Function

File_ReadCSVToArray

Public Shared Function ReadCSVToArray(ByVal sFolderPath As String, _
Optional ByVal sFileName As String = "", _
Optional ByVal sExtension As String = ".csv") _
As String(,)
Try
If gbEND_GENERAL = True Then Exit Function

Dim regRegex As System.Text.RegularExpressions.Regex
Dim sfilecontents As String
Dim slineoftext As String
Dim saarraytemp As String()

sfilecontents = clsFile.ReadToString(sFolderPath, sFileName, sExtension)

regRegex = New System.Text.RegularExpressions.Regex("\r")

For Each slineoftext In regRegex.Split(sfilecontents)

'only get the first line of data
saarraytemp = slineoftext.Split(","c)
Exit For

Next slineoftext

Catch objCOMException As System.Runtime.InteropServices.COMException
mobjCOMException = objCOMException
Catch objException As Exception
mobjException = objException

Finally
If gbDEBUG_GENERAL = True Or _
((Not mobjCOMException Is Nothing) Or (Not mobjException Is Nothing)) Then

Call clsError.Handle("ReadCSVToArray", msCLASSNAME, _
".", _
mobjCOMException, mobjException)
End If
End Try
End Function

File_ReadToString

Public Shared Function ReadToString(ByVal sFolderPath As String, _
Optional ByVal sFileName As String = "", _
Optional ByVal sExtension As String = "", _
Optional ByVal bCheckFileExists As Boolean = True) _
As String

Try
If gbEND_GENERAL = True Then Exit Function

Dim objStreamReader As System.IO.StreamReader
Dim sfullpath As String

sfullpath = sFolderPath & sFileName & sExtension

If bCheckFileExists = True Then
If clsFile.Exists(sfullpath) = False Then
Call clszMessagesGeneral.FileDoesNotExistWarning(sfullpath, "")

ReadToString = "Invalid File"
Exit Function
End If
End If

objStreamReader = New System.IO.StreamReader(sfullpath)

ReadToString = objStreamReader.ReadToEnd

objStreamReader.Close()

Catch objCOMException As System.Runtime.InteropServices.COMException
mobjCOMException = objCOMException
Catch objException As Exception
mobjException = objException

Finally
If gbDEBUG_GENERAL = True Or _
((Not mobjCOMException Is Nothing) Or (Not mobjException Is Nothing)) Then

Call clsError.Handle("ReadToString", msCLASSNAME, _
"return the contents of this file as a string.", _
mobjCOMException, mobjException)
End If
End Try
End Function

File_SizeInBytes

Public Function File_SizeInBytes(ByVal sFileFullPath As String) As Long

Dim objFileInfo As System.IO.FileInfo

Try
If My.Settings.USER_ERROR_OCCURRED = True Then Exit Function

If modGeneral.File_Exists(sFileFullPath, True) Then
objFileInfo = New System.IO.FileInfo(sFileFullPath)

Return objFileInfo.Length
Else
Return 0
End If

Catch ex As System.Exception
Call modMessages.ErrorMessage(System.Reflection.MethodBase.GetCurrentMethod, ex.Message)
Return 0
End Try
End Function

File_ToByteArray

Public Function File_ToByteArray(ByVal sFilePath As String) As Byte()

Dim fStream As System.IO.FileStream
Dim fBinary As Byte()

Try
fStream = New System.IO.FileStream(sFilePath, _
System.IO.FileMode.Open, _
System.IO.FileAccess.Read, _
System.IO.FileShare.ReadWrite)

ReDim fBinary(CType(fStream.Length, Integer))

fStream.Read(fBinary, 0, CType(fStream.Length, Integer))
fStream.Close()

Return fBinary

Catch ex As Exception
Call modMessages.ErrorMessage(System.Reflection.MethodBase.GetCurrentMethod, ex.Message)
Return Nothing
End Try
End Function

Public Function File_ToByteArray2(ByVal myFilePath As String) As Byte()

Dim objByteArray As System.Byte()
Dim objFileStream As System.IO.FileStream
Dim sTemporaryFile As String
Dim iDotPos As Integer

Try

iDotPos = Microsoft.VisualBasic.InStrRev(myFilePath, ".")

sTemporaryFile = Microsoft.VisualBasic.Environ("USERPROFILE") & "\DocUploadCopy." &
Microsoft.VisualBasic.Right(myFilePath, (myFilePath.Length - iDotPos))

Call modLateBinding.File_Copy(myFilePath, sTemporaryFile)

objFileStream = New System.IO.FileStream(sTemporaryFile, System.IO.FileMode.Open, System.IO.FileAccess.Read)

ReDim objByteArray(CType(objFileStream.Length, Integer))
objFileStream.Read(objByteArray, 0, CType(objFileStream.Length, Integer))

objFileStream.Close()
Return objByteArray

Catch ex As System.Exception
Call modMessages.ErrorMessage(System.Reflection.MethodBase.GetCurrentMethod, ex.Message)
Return Nothing
Finally
objFileStream = Nothing
End Try
End Function

File_ToString

Public Function File_ToString(ByVal sFilePath As String) As String
Dim objStreamReader As System.IO.StreamReader

Try
objStreamReader = New System.IO.StreamReader(sFilePath)
Return objStreamReader.ReadToEnd

Catch ex As Exception
Call modMessages.ErrorMessage(System.Reflection.MethodBase.GetCurrentMethod, ex.Message)
Return ""
End Try
End Function

File_TypeReturn

Public Shared Function TypeReturn(ByVal sFileExtension As String) _
As String

Try
If gbEND_GENERAL = True Then Exit Function

Select Case sFileExtension
Case ".bmp" : TypeReturn = "Bitmap Image"
Case ".css" : TypeReturn = "CSS File"
Case ".doc" : TypeReturn = "Microsoft Word Document"
Case ".dot" : TypeReturn = "Microsoft Word Template"
Case ".htm" : TypeReturn = "HTML Document"
Case ".ico" : TypeReturn = "Icon"
Case ".png" : TypeReturn = "Macromedia Fireworks Doc"
Case ".ppt" : TypeReturn = "Microsoft PowerPoint Presentation"
Case ".txt" : TypeReturn = "Text Document"
Case ".xla" : TypeReturn = "Microsoft Excel Add-in"
Case ".xls" : TypeReturn = "Microsoft Excel Workbook"
Case ".xlt" : TypeReturn = "Microsoft Excel Template"

Case Else : TypeReturn = "Unknown"

End Select

Catch objCOMException As System.Runtime.InteropServices.COMException
mobjCOMException = objCOMException
Catch objException As Exception
mobjException = objException

Finally
If gbDEBUG_GENERAL = True Or _
((Not mobjCOMException Is Nothing) Or (Not mobjException Is Nothing)) Then

Call clsError.Handle("TypeReturn", msCLASSNAME, _
"", _
mobjCOMException, mobjException)
End If
End Try
End Function

Folder_Accessible

Public Shared Function Accessible(ByVal sFolderPath As String) As Boolean

Try
If gbEND_GENERAL = True Then Exit Function

Dim objdirectoryinfo As New System.IO.DirectoryInfo(sFolderPath)

Accessible = False

If objdirectoryinfo.GetDirectories.Length = 0 Or _
objdirectoryinfo.GetDirectories.Length <> 0 Then

Accessible = True
End If

Catch objCOMException As System.Runtime.InteropServices.COMException
Catch objException As Exception

End Try
End Function

Folder_BrowseReturn

Public Shared Sub BrowseReturn(ByVal sNewFolderPath As String, _
ByVal sSearchPattern As String, _
ByVal txtFolderPath As System.Windows.Forms.TextBox, _
ByVal cboFileName As System.Windows.Forms.ComboBox)

Try
If gbEND_GENERAL = True Then Exit Sub

Dim safiles() As String

sNewFolderPath = clsFolder.LineAdd(sNewFolderPath)

txtFolderPath.Text = sNewFolderPath
Call clsFolder.FilesToArraySingle(sNewFolderPath, "", safiles, sSearchPattern, False)

If safiles Is Nothing Then
cboFileName.Items.Clear()
cboFileName.Items.Add("No files")
cboFileName.SelectedIndex = 0
Exit Sub
Else
Call clsArray.RemoveChars("array", safiles, sNewFolderPath.Length, 0)
Call clsArray.ToListComboBox(cboFileName, "", safiles)

Call clsFile.ListComboSelect(cboFileName)
End If

Catch objCOMException As System.Runtime.InteropServices.COMException
gobjCOMException = objCOMException
Catch objException As Exception
gobjException = objException

Finally
If gbDEBUG_GENERAL = True Or _
((IsNothing(gobjCOMException) = False Or IsNothing(gobjException) = False)) Then

Call clsError.Handle("BrowseReturn", "clsFolder", _
"transfer the files in the folder to the array ?", _
gobjCOMException, gobjException)
End If
End Try
End Sub

Folder_Create

Creates a folder. This will create sub folders if necessary as it starts at the top level.
Public Function Folder_Create(ByVal sFolderPath As String) As Boolean

Try
If My.Settings.USER_ERROR_OCCURRED = True Then Exit Function

If modGeneral.Folder_Exists(sFolderPath) = False Then
System.IO.Directory.CreateDirectory(sFolderPath)
End If
Return True

Catch ex As System.Exception
Call modMessages.ErrorMessage(System.Reflection.MethodBase.GetCurrentMethod, ex.Message)
Return False
End Try

End Function

Folder_Exists

Public Shared Function Exists(ByVal sFolderPath As String, _
Optional ByVal bInformUser As Boolean = False) _
As Boolean

Dim objDirectoryInfo As System.IO.DirectoryInfo

Try
If gbEND_GENERAL = True Then Exit Function

objDirectoryInfo = New System.IO.DirectoryInfo(sFolderPath)

Exists = objDirectoryInfo.Exists

If Exists = False And bInformUser = True Then
Call MsgBox("The File or Folder does not exists : " & vbCrLf & _
"""" & sFolderPath & """")
End If

Catch objCOMException As System.Runtime.InteropServices.COMException
gobjCOMException = objCOMException
Catch objException As Exception
gobjException = objException

Finally
If gbDEBUG_GENERAL = True Or _
((IsNothing(gobjCOMException) = False Or IsNothing(gobjException) = False)) Then

Call clsError.Handle("Exists", "clsFolder", _
"determine if this folder actually exists:" & vbCrLf & _
"'" & sFolderPath & "'." & _
vbCrLf & vbCrLf & "There may be an invalid character.", _
gobjCOMException, gobjException)
End If
End Try
End Function

Folder_FilesHasAny

Public Shared Function FilesHasAny(ByVal sFolderPath As String, _
Optional ByVal bCheckValid As Boolean = True, _
Optional ByVal bCheckLineAdd As Boolean = False, _
Optional ByVal sSearchPattern As String = "") _
As Boolean

Try
If gbEND_GENERAL = True Then Exit Function

Dim objfileinfo As New System.IO.DirectoryInfo(sFolderPath)

If objfileinfo.GetFiles.Length = 0 Then FilesHasAny = False
If objfileinfo.GetFiles.Length > 0 Then FilesHasAny = True

Catch objCOMException As System.Runtime.InteropServices.COMException
gobjCOMException = objCOMException
Catch objException As Exception
gobjException = objException

Finally
If gbDEBUG_GENERAL = True Or _
((IsNothing(gobjCOMException) = False Or IsNothing(gobjException) = False)) Then

Call clsError.Handle("FilesHasAny", "clsFolder", _
"determine if there are any files in the folder path:" & _
vbCrLf & "'" & sFolderPath & "'.", _
gobjCOMException, gobjException)
End If
End Try
End Function

Folder_FilesToArraySingle

Public Shared Sub FilesToArraySingle(ByVal sFolderPath As String, _
ByVal sArrayName As String, _
ByRef asArrayName() As String, _
Optional ByVal sSearchPattern As String = "*", _
Optional ByVal bInformUser As Boolean = True, _
Optional ByVal bAddThreeDots As Boolean = False)

Try
If gbEND_GENERAL = True Then Exit Sub

Dim itotalfiles As Integer
Dim ifoldernumber As Integer
Dim snextitem As String

If clsFolder.FilesHasAny(sFolderPath, False) = True Then

asArrayName = System.IO.Directory.GetFiles(sFolderPath, sSearchPattern)

If asArrayName.Length = 0 Then asArrayName = Nothing

Else
asArrayName = Nothing

If bInformUser = True Then
Call MsgBox("There are no folders in this folder !")
End If
End If

Catch objCOMException As System.Runtime.InteropServices.COMException
gobjCOMException = objCOMException
Catch objException As Exception
gobjException = objException

Finally
If gbDEBUG_GENERAL = True Or _
((IsNothing(gobjCOMException) = False Or IsNothing(gobjException) = False)) Then

Call clsError.Handle("FilesToArraySingle", "clsFolder", _
"transfer the list of all the files in the folder path:" & _
vbCrLf & "'" & sFolderPath & "'" & _
"to the single dimensional array '" & sArrayName & "'", _
gobjCOMException, gobjException)
End If
End Try
End Sub

Folder_FilesToComboBox

Public Shared Sub FilesToComboBox(ByVal sFolderPath As String, _
ByVal objComboBox As System.Windows.Forms.ComboBox, _
Optional ByVal sSearchPattern As String = "*", _
Optional ByVal bInformUser As Boolean = True, _
Optional ByVal bAddThreeDots As Boolean = False)

Try
If gbEND_GENERAL = True Then Exit Sub

Dim asfiles() As String
Dim iarraycount As Integer

If clsFolder.FilesHasAny(sFolderPath, False) = True Then

asfiles = System.IO.Directory.GetFiles(sFolderPath, sSearchPattern)

Call clsArray.RemoveChars("array", asfiles, sFolderPath.Length, 0)

For iarraycount = 0 To asfiles.Length - 1

If (asfiles(iarraycount).ToString.Length > 0) Then
objComboBox.Items.Add(CStr(asfiles(iarraycount)))
End If

Next iarraycount

Else

If bInformUser = True Then
Call clszMessagesGeneral.FolderNoFilesInformation(sFolderPath)
End If
End If

Catch objCOMException As System.Runtime.InteropServices.COMException
mobjCOMException = objCOMException
Catch objException As Exception
mobjException = objException

Finally
If gbDEBUG_GENERAL = True Or _
((Not mobjCOMException Is Nothing) Or (Not mobjException Is Nothing)) Then

Call clsError.Handle("FilesToComboBox", msCLASSNAME, _
"transfer the list of all the files in the folder path:" & _
gsCRLF & "'" & sFolderPath & "'." & _
"to the combo list box'" & "??" & "'", _
mobjCOMException, mobjException)
End If
End Try
End Sub

Folder_FilesToStringConCat

Public Shared Function FilesToStringConCat(ByVal sFolderPath As String, _
Optional ByVal sSearchPattern As String = "*", _
Optional ByVal bInformUser As Boolean = True, _
Optional ByVal sSeperatorChar As String = ";") As String

Try
If gbEND_GENERAL = True Then Exit Function

Dim asfilesarray() As String
Dim itotalfiles As Integer
Dim ifoldernumber As Integer
Dim snextitem As String

If clsFolder.FilesHasAny(sFolderPath, False) = True Then

asfilesarray = System.IO.Directory.GetFiles(sFolderPath, sSearchPattern)

If asfilesarray.Length > 0 Then
FilesToStringConCat = clsArray.ToStringConCat("FilesToString", asfilesarray, sSeperatorChar)
Else
FilesToStringConCat = ""
End If

Else
asfilesarray = Nothing

If bInformUser = True Then
Call MsgBox("There are no folders in this folder !")
End If
End If

Catch objCOMException As System.Runtime.InteropServices.COMException
gobjCOMException = objCOMException
Catch objException As Exception
gobjException = objException

Finally
If gbDEBUG_GENERAL = True Or _
((IsNothing(gobjCOMException) = False Or IsNothing(gobjException) = False)) Then

Call clsError.Handle("FilesToArraySingle", "clsFolder", _
"transfer the list of all the files in the folder path:" & _
vbCrLf & "'" & sFolderPath & "'" & _
"to the string concatenation.", _
gobjCOMException, gobjException)
End If
End Try
End Function

Folder_LineAdd

public static string Folder_LineAdd(
string sFolderPath)
{
try
{
if (sFolderPath.Length > 0)
{
if (sFolderPath.Substring(sFolderPath.Length - 1, 1) != @"\")
{
return sFolderPath + @"\";
}
else
{
return sFolderPath;
}
}
return sFolderPath;
}
catch (System.Exception)
{
return sFolderPath;
}
}
Public Shared Function LineAdd(ByVal sFolderPath As String) _
As String

Try
If gbEND_GENERAL = True Then Exit Function

If sFolderPath <> "" Then
If sFolderPath.Substring(sFolderPath.Length - 1, 1) <> "\" Then
LineAdd = sFolderPath & "\"
Else
LineAdd = sFolderPath
End If
End If

Catch objCOMException As System.Runtime.InteropServices.COMException
gobjCOMException = objCOMException
Catch objException As Exception
gobjException = objException

Finally
If gbDEBUG_GENERAL = True Or _
((IsNothing(gobjCOMException) = False Or IsNothing(gobjException) = False)) Then

Call clsError.Handle("LineAdd", "clsFolder", _
"add the character ""\"" from the end of the folder path:" & _
vbCrLf & "'" & sFolderPath & "'.", _
gobjCOMException, gobjException)
End If
End Try
End Function

Folder_LineRemove

Public Shared Function LineRemove(ByVal sFolderPath As String) As String

Try
If gbEND_GENERAL = True Then Exit Function

If sFolderPath <> "" Then
If sFolderPath.Substring(sFolderPath.Length - 1, 1) = "\" Then
LineRemove = sFolderPath.Substring(0, sFolderPath.Length - 1)
Else
LineRemove = sFolderPath
End If
End If

Catch objCOMException As System.Runtime.InteropServices.COMException
gobjCOMException = objCOMException
Catch objException As Exception
gobjException = objException

Finally
If gbDEBUG_GENERAL = True Or _
((IsNothing(gobjCOMException) = False Or IsNothing(gobjException) = False)) Then

Call clsError.Handle("LineRemove", "clsFolder", _
"remove the character ""\"" from the end of the folder path:" & _
vbCrLf & "'" & sFolderPath & "'.", _
gobjCOMException, gobjException)
End If
End Try
End Function

Folder_Network_IsAvailable

Function Network_IsAvailable() As Boolean
Try
If My.Settings.USER_ERROR_OCCURRED = True Then Exit Function

Return System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable

Catch ex As System.Exception

End Try
End Function

Folder_SubFoldersHasAny

Public Shared Function SubFoldersHasAny(ByVal sFolderPath As String, _
Optional ByVal bCheckLineAdd As Boolean = False, _
Optional ByVal bInformUser As Boolean = False) _
As Boolean

Try
If gbEND_GENERAL = True Then Exit Function


Dim objfileinfo As New System.IO.DirectoryInfo(sFolderPath)

If objfileinfo.GetDirectories.Length = 0 Then SubFoldersHasAny = False
If objfileinfo.GetDirectories.Length > 0 Then SubFoldersHasAny = True

Catch objCOMException As System.Runtime.InteropServices.COMException
gobjCOMException = objCOMException
Catch objException As Exception
gobjException = objException

Finally
If gbDEBUG_GENERAL = True Or _
((IsNothing(gobjCOMException) = False Or IsNothing(gobjException) = False)) Then

Call clsError.Handle("SubFoldersHasAny", "clsFolder", _
"determine if there are any sub folders in the folder path:" & _
vbCrLf & "'" & sFolderPath & "'.", _
gobjCOMException, gobjException)
End If
End Try
End Function

Folder_SubFoldersToArray

Public Shared Sub SubFoldersToArray(ByVal sFolderPath As String, _
ByVal sArrayName As String, _
ByRef asFoldersArray() As String, _
Optional ByVal bAddLine As Boolean = True, _
Optional ByVal bRemoveHidden As Boolean = True, _
Optional ByVal bInformUser As Boolean = True)

Try
If gbEND_GENERAL = True Then Exit Sub

Dim asdirectories() As String
Dim itotalfiles As Integer
Dim ifoldernumber As Integer
Dim snextitem As String

If clsFolder.SubFoldersHasAny(sFolderPath, False) = True Then

asdirectories = System.IO.Directory.GetDirectories(sFolderPath)
asFoldersArray = asdirectories

If bAddLine = True Then Call clsArray.AddAfter("\", sArrayName, asFoldersArray)

If bRemoveHidden = True Then Call clsArray.RemoveHiddenFolders(asFoldersArray)

Else
If bInformUser = True Then
Call MsgBox("There are no folders in this folder !")
End If
End If

Catch objCOMException As System.Runtime.InteropServices.COMException
gobjCOMException = objCOMException
Catch objException As Exception
gobjException = objException

Finally
If gbDEBUG_GENERAL = True Or _
((IsNothing(gobjCOMException) = False Or IsNothing(gobjException) = False)) Then

Call clsError.Handle("SubFoldersToArray", "clsFolder", _
"transfer the list of sub folders in the folder path:" & _
vbCrLf & "'" & sFolderPath & "'." & _
"to the single dimensional array '" & sArrayName & "'", _
gobjCOMException, gobjException)
End If
End Try
End Sub

Folder_TreeViewFolderSelect

Public Shared Sub Folder_TreeViewFolderSelect(ByRef gbAddFolders As Boolean, _
ByVal objTreeView As TreeView, _
ByVal sNodePath As String, _
ByVal objTreeNodeColl As TreeNodeCollection)

Try
If gbEND_GENERAL = True Then Exit Sub

Dim inextlevel As Integer
Dim snodetext As String
Dim objnode As TreeNode

If InStr(sNodePath, "\") > 0 Then
snodetext = sNodePath.Substring(0, InStr(sNodePath, "\") - 1)

For Each objnode In objTreeNodeColl
If objnode.Text = snodetext Then

gbAddFolders = False
objTreeView.SelectedNode = objnode
gbAddFolders = True

Call clsTreeView.FoldersAdd(objnode)

Call clsTreeView.FolderSelect(gbAddFolders, objTreeView, _
sNodePath.Substring(snodetext.Length + 1), _
objnode.Nodes)
Exit Sub
End If
Next

Else
For Each objnode In objTreeNodeColl
If objnode.Text = sNodePath Then
gbAddFolders = False
objTreeView.SelectedNode = objnode
gbAddFolders = True
Exit Sub
End If
Next
End If

'-------------- sub folder found is no longer there, expand to show
If Not objTreeView.SelectedNode Is Nothing Then
objTreeView.SelectedNode.Expand()
End If

Catch objCOMException As System.Runtime.InteropServices.COMException
gobjCOMException = objCOMException
Catch objException As Exception
gobjException = objException

Finally
If gbDEBUG_GENERAL = True Or _
((IsNothing(gobjCOMException) = False Or IsNothing(gobjException) = False)) Then

Call clsError.Handle("FolderSelect", "clsTreeView", _
"select this folder in the tree view.", _
gobjCOMException, gobjException)
End If
End Try
End Sub

Folders_ArrayRemoveHidden

Public Sub Folders_ArrayRemoveHidden(ByRef asFoldersArray() As String)

On Error GoTo AnError

Dim iarraycount As Integer
Dim ifolderscount As Integer
Dim astemp() As String
Dim itempcount As Integer

For iarraycount = 0 To asFoldersArray.Length - 1
Dim objfileinfo As New System.IO.DirectoryInfo(asFoldersArray(iarraycount))

If (objfileinfo.Attributes And IO.FileAttributes.Hidden) <> _
IO.FileAttributes.Hidden Then
ifolderscount = ifolderscount + 1
End If
Next iarraycount

ReDim astemp(ifolderscount - 1)

For iarraycount = 0 To asFoldersArray.Length - 1
Dim objfileinfo As New System.IO.DirectoryInfo(asFoldersArray(iarraycount))

If (objfileinfo.Attributes And IO.FileAttributes.Hidden) <> _
IO.FileAttributes.Hidden Then

astemp(itempcount) = asFoldersArray(iarraycount)
itempcount = itempcount + 1
End If

Next iarraycount

ReDim asFoldersArray(astemp.Length - 1)
asFoldersArray = astemp


If gbDEBUG = False Then Exit Sub
AnError:
Call Error_Handle("Folders_ArrayRemoveHidden", msMODULENAME, _
"remove the hidden folders from the array")
End Sub

Message_FileBeingUsedByAnotherProcess

Public Shared Sub Message_FileBeingUsedByAnotherProcess(ByVal sFolderPath As String,
Optional ByVal sFileName As String = "",
Optional ByVal sExtension As String = "",
Optional ByVal bFlagError As Boolean = False)

'If clsError.ErrorFlag() = True Then Exit Sub

Call System.Windows.Forms.MessageBox.Show(
"This file is being used by another process:" &
System.Environment.NewLine &
"'" & sFolderPath & sFileName & sExtension & "'",
gobjSolution.SolutionFormTitle,
System.Windows.Forms.MessageBoxButtons.OK,
System.Windows.Forms.MessageBoxIcon.Warning)

'sets the flag to an error
clsError.ErrorFlagChange(bFlagError)
End Sub

Message_FileDoesNotExist

Public Shared Sub Message_FileDoesNotExist(ByVal sFolderPath As String,
Optional ByVal sFileName As String = "",
Optional ByVal sExtension As String = "",
Optional ByVal bFlagError As Boolean = False)

'If clsError.ErrorFlag() = True Then Exit Sub

Call System.Windows.Forms.MessageBox.Show(
"The following file does not exist :" &
System.Environment.NewLine &
System.Environment.NewLine &
"'" & sFolderPath & sFileName & sExtension & "'",
gobjSolution.SolutionFormTitle,
System.Windows.Forms.MessageBoxButtons.OK,
System.Windows.Forms.MessageBoxIcon.Warning)

'sets the flag to an error
clsError.ErrorFlagChange(bFlagError)
End Sub

Message_FileHasBeenSaved

Public Shared Sub Message_FileHasBeenSaved(ByVal sFolderPath As String,
Optional ByVal sFileName As String = "",
Optional ByVal sExtension As String = "",
Optional ByVal bFlagError As Boolean = False)

'If clsError.ErrorFlag() = True Then Exit Sub

Call System.Windows.Forms.MessageBox.Show(
"The following file has been saved :" &
System.Environment.NewLine &
System.Environment.NewLine &
"'" & sFolderPath & sFileName & sExtension & "'",
gobjSolution.SolutionFormTitle,
System.Windows.Forms.MessageBoxButtons.OK,
System.Windows.Forms.MessageBoxIcon.Information)

'sets the flag to an error
clsError.ErrorFlagChange(bFlagError)
End Sub

Message_FolderDoesNotExist

Public Shared Sub Message_FolderDoesNotExist(ByVal sFolderPath As String,
Optional ByVal bFlagError As Boolean = False)

'If clsError.ErrorFlag() = True Then Exit Sub

Call System.Windows.Forms.MessageBox.Show(
"The following folder does not exist :" &
System.Environment.NewLine &
System.Environment.NewLine &
"'" & sFolderPath & "'",
gobjSolution.SolutionFormTitle,
System.Windows.Forms.MessageBoxButtons.OK,
System.Windows.Forms.MessageBoxIcon.Warning)

'sets the flag to an error
clsError.ErrorFlagChange(bFlagError)
End Sub

Message_FolderNoFiles

Public Shared Sub Message_FolderNoFiles(ByVal sFolderPath As String,
Optional ByVal bFlagError As Boolean = False)

'If clsError.ErrorFlag() = True Then Exit Sub

Call System.Windows.Forms.MessageBox.Show(
"The following folder does not contain any files :" &
System.Environment.NewLine &
System.Environment.NewLine &
"'" & sFolderPath & "'",
gobjSolution.SolutionFormTitle,
System.Windows.Forms.MessageBoxButtons.OK,
System.Windows.Forms.MessageBoxIcon.Information)

'sets the flag to an error
clsError.ErrorFlagChange(bFlagError)
End Sub

Message_FolderNoSubFolders

Public Shared Sub Message_FolderNoSubFolders(ByVal sFolderPath As String,
Optional ByVal bFlagError As Boolean = False)

'If clsError.ErrorFlag() = True Then Exit Sub

Call System.Windows.Forms.MessageBox.Show(
"The following folder does not contain any sub folders :" &
System.Environment.NewLine &
System.Environment.NewLine &
"'" & sFolderPath & "'",
gobjSolution.SolutionFormTitle,
System.Windows.Forms.MessageBoxButtons.OK,
System.Windows.Forms.MessageBoxIcon.Information)

'sets the flag to an error
clsError.ErrorFlagChange(bFlagError)
End Sub

Question_FileDelete

Public Shared Function Question_FileDelete(ByVal sFolderPath As String,
Optional ByVal sFileName As String = "",
Optional ByVal sExtension As String = "",
Optional ByVal iDefaultButton As System.Int32 = 2,
Optional ByVal bFlagError As Boolean = False) _
As Boolean

Dim breturn As Boolean
Dim objreturn As System.Windows.Forms.DialogResult
Dim objdefaultbutton As System.Windows.Forms.MessageBoxDefaultButton

Select Case iDefaultButton
Case 1 : objdefaultbutton = System.Windows.Forms.MessageBoxDefaultButton.Button1
Case 2 : objdefaultbutton = System.Windows.Forms.MessageBoxDefaultButton.Button2
End Select

objreturn = System.Windows.Forms.MessageBox.Show(
"Are you sure you want to remove the following file :" &
System.Environment.NewLine &
sFolderPath & sFileName & sExtension,
gobjSolution.SolutionFormTitle & " - Delete File",
System.Windows.Forms.MessageBoxButtons.YesNo,
System.Windows.Forms.MessageBoxIcon.Question,
objdefaultbutton)

If objreturn = System.Windows.Forms.DialogResult.Yes Then breturn = True
If objreturn = System.Windows.Forms.DialogResult.No Then breturn = False

'sets the flag to an error
clsError.ErrorFlagChange(bFlagError)

Return breturn
End Function

Question_FileReplace

Public Shared Function Question_FileReplace(ByVal sFolderPath As String,
Optional ByVal sFileName As String = "",
Optional ByVal sExtension As String = "",
Optional ByVal iDefaultButton As System.Int32 = 2,
Optional ByVal bFlagError As Boolean = False) _
As Boolean

Dim breturn As Boolean
Dim objreturn As System.Windows.Forms.DialogResult
Dim objdefaultbutton As System.Windows.Forms.MessageBoxDefaultButton

Select Case iDefaultButton
Case 1 : objdefaultbutton = System.Windows.Forms.MessageBoxDefaultButton.Button1
Case 2 : objdefaultbutton = System.Windows.Forms.MessageBoxDefaultButton.Button2
End Select

objreturn = System.Windows.Forms.MessageBox.Show(
"This file already exists :" &
System.Environment.NewLine &
sFolderPath & sFileName & sExtension &
System.Environment.NewLine &
System.Environment.NewLine &
"Do you want to replace it ?",
gobjSolution.SolutionFormTitle & " - Replace File",
System.Windows.Forms.MessageBoxButtons.YesNo,
System.Windows.Forms.MessageBoxIcon.Question,
objdefaultbutton)

If objreturn = System.Windows.Forms.DialogResult.Yes Then breturn = True
If objreturn = System.Windows.Forms.DialogResult.No Then breturn = False

'sets the flag to an error
clsError.ErrorFlagChange(bFlagError)

Return breturn
End Function

Question_FolderDelete

Public Shared Function Question_FolderDelete(ByVal sFoldersToDelete As String,
Optional ByVal sSeperatorChar As String = ";",
Optional ByVal iDefaultButton As System.Int32 = 2,
Optional ByVal bFlagError As Boolean = False) _
As Boolean
Dim sfoldertext As String
Dim breturn As Boolean
Dim objreturn As System.Windows.Forms.DialogResult
Dim objdefaultbutton As System.Windows.Forms.MessageBoxDefaultButton

If sFoldersToDelete.IndexOf(sSeperatorChar) > 0 Then
sFoldersToDelete = sFoldersToDelete.Replace(sSeperatorChar, System.Environment.NewLine)
sfoldertext = "folders"
Else
sfoldertext = "folder"
End If

Select Case iDefaultButton
Case 1 : objdefaultbutton = System.Windows.Forms.MessageBoxDefaultButton.Button1
Case 2 : objdefaultbutton = System.Windows.Forms.MessageBoxDefaultButton.Button2
End Select

objreturn = System.Windows.Forms.MessageBox.Show(
"Are you sure you want to remove the following " & sfoldertext & ":" &
System.Environment.NewLine &
System.Environment.NewLine &
sFoldersToDelete,
gobjSolution.SolutionFormTitle & " - Delete Folder",
System.Windows.Forms.MessageBoxButtons.YesNo,
System.Windows.Forms.MessageBoxIcon.Question,
objdefaultbutton)

If objreturn = System.Windows.Forms.DialogResult.Yes Then breturn = True
If objreturn = System.Windows.Forms.DialogResult.No Then breturn = False

'sets the flag to an error
clsError.ErrorFlagChange(bFlagError)

Return breturn
End Function

© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited Top