Code Snippets
File_AttributesDisplay
public static void File_AttributesDisplay(
string sFolderPath,
string sFileName = "",
string sExtension = "")
{
System.IO.FileInfo objfileinfo = new System.IO.FileInfo(sFolderPath + sFileName + sExtension);
string sfileattributes = null;
try
{
if (gbEND_GENERAL == true)
return;
if (objfileinfo.Attributes == System.IO.FileAttributes.Archive)
sfileattributes += "Archive,";
if (objfileinfo.Attributes == System.IO.FileAttributes.Compressed)
sfileattributes += "Compressed,";
if (objfileinfo.Attributes == System.IO.FileAttributes.Device)
sfileattributes += "Device,";
if (objfileinfo.Attributes == System.IO.FileAttributes.Directory)
sfileattributes += "Directory,";
if (objfileinfo.Attributes == System.IO.FileAttributes.Encrypted)
sfileattributes += "Encrypted,";
if (objfileinfo.Attributes == System.IO.FileAttributes.Hidden)
sfileattributes += "Hidden,";
if (objfileinfo.Attributes == System.IO.FileAttributes.Normal)
sfileattributes += "Normal,";
if (objfileinfo.Attributes == System.IO.FileAttributes.NotContentIndexed)
sfileattributes += "NotContentIndexed,";
if (objfileinfo.Attributes == System.IO.FileAttributes.Offline)
sfileattributes += "Offline,";
if (objfileinfo.Attributes == System.IO.FileAttributes.ReadOnly)
sfileattributes += "ReadOnly,";
if (objfileinfo.Attributes == System.IO.FileAttributes.ReparsePoint)
sfileattributes += "ReparsePoint,";
if (objfileinfo.Attributes == System.IO.FileAttributes.SparseFile)
sfileattributes += "SparseFile,";
if (objfileinfo.Attributes == System.IO.FileAttributes.System)
sfileattributes += "System,";
if (objfileinfo.Attributes == System.IO.FileAttributes.Temporary)
sfileattributes += "Temporary,";
System.Windows.Forms.MessageBox.Show(
sfileattributes,
gsDIALOG_PREFIX_GENERAL + "File Attributes",
System.Windows.Forms.MessageBoxButtons.OK,
System.Windows.Forms.MessageBoxIcon.Information);
}
catch (System.Runtime.InteropServices.COMException ex)
{
mobjCOMException = ex;
}
catch (Exception ex)
{
mobjException = ex;
}
finally
{
if (gbDEBUG_GENERAL == true ||
(mobjCOMException != null || mobjException != null))
{
clsError.Handle(
"AttributesDisplay",
msCLASSNAME,
"display all the attributes for the following file:" +
gsCRLF + "'" + sFolderPath + sFileName + sExtension + "'.",
mobjCOMException,
mobjException);
}
}
}
File_BrowseReturn
public static void File_BrowseReturn(
string sNewFolderPath,
string sSearchPattern,
System.Windows.Forms.TextBox txtFolderPath,
System.Windows.Forms.ComboBox cboFileName)
{
try
{
if (gbEND_GENERAL == true)
return;
string[] safiles;
sNewFolderPath = clsFolder.LineAdd(sNewFolderPath);
txtFolderPath.Text = sNewFolderPath;
clsFolder.FilesToArraySingle(sNewFolderPath, "", out safiles, sSearchPattern, false);
if (safiles == null)
{
cboFileName.Items.Clear();
cboFileName.Items.Add("No files");
cboFileName.SelectedIndex = 0;
return;
}
else
{
clsArray.RemoveChars("array", safiles, sNewFolderPath.Length, 0);
clsArray.ToListComboBox(cboFileName, "", safiles);
clsFile.ListComboSelect(cboFileName);
}
}
catch (System.Runtime.InteropServices.COMException ex)
{
gobjCOMException = ex;
}
catch (Exception ex)
{
gobjException = ex;
}
finally
{
if (gbDEBUG_GENERAL == true ||
(gobjCOMException != null || gobjException != null))
{
clsError.Handle(
"BrowseReturn",
"clsFolder",
"transfer the files in the folder to the array ?",
gobjCOMException,
gobjException);
}
}
}
File_ComboBoxFileSelect
public static void File_ComboBoxFileSelect(
System.Windows.Forms.ComboBox objComboBox)
{
try
{
if (gbEND_GENERAL == true)
return;
switch (objComboBox.Items.Count)
{
case 0:
objComboBox.Items.Insert(0, "No files");
objComboBox.SelectedIndex = 0;
break;
case 1:
objComboBox.SelectedIndex = 0;
break;
default:
objComboBox.Items.Insert(0, "(" + objComboBox.Items.Count + " files)");
objComboBox.SelectedIndex = 0;
break;
}
}
catch (System.Runtime.InteropServices.COMException ex)
{
mobjCOMException = ex;
}
catch (Exception ex)
{
mobjException = ex;
}
finally
{
if (gbDEBUG_GENERAL == true ||
(mobjCOMException != null || mobjException != null))
{
clsError.Handle(
"ComboBoxFileSelect",
msCLASSNAME,
"select the file in the combo box.",
mobjCOMException,
mobjException);
}
}
}
File_ContentsRemoveString
public static void File_ContentsRemoveString(
string sRemoveString,
string sFolderPath,
string sFileName = "",
string sExtension = "")
{
try
{
if (gbEND_GENERAL == true)
return;
System.IO.StreamReader objStreamReader;
System.IO.StreamWriter objStreamWriter;
string sfullpath;
string stextcontents;
sfullpath = sFolderPath + sFileName + sExtension;
if (clsFile.Exists(sfullpath) == false)
{
clszMessagesGeneral.FileDoesNotExistWarning(sfullpath, "");
return;
}
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 (System.Runtime.InteropServices.COMException ex)
{
mobjCOMException = ex;
}
catch (Exception ex)
{
mobjException = ex;
}
finally
{
if (gbDEBUG_GENERAL == true ||
(mobjCOMException != null || mobjException != null))
{
clsError.Handle(
"ContentsRemoveString",
msCLASSNAME,
"remove the string '" + sRemoveString + "' from the text file.",
mobjCOMException,
mobjException);
}
}
}
File_Copy
Copies a file from one folder to another.public static bool File_Copy(
string strSourcePath,
string strDestinationPath)
{
try
{
if (My.Settings.USER_ERROR_OCCURRED == true)
return false;
if (modGeneral.File_Exists(strSourcePath, true))
{
System.IO.File.Copy(strSourcePath, strDestinationPath);
return true;
}
else
{
return false;
}
}
catch (System.Exception ex)
{
modMessages.ErrorMessage(System.Reflection.MethodBase.GetCurrentMethod(), ex.Message);
return false;
}
}
File_CopyTo
public static bool File_CopyTo(
string sFolderFrom,
string sFileName,
string sFolderTo,
string sExtension = "",
string sNewFileName = "",
bool bOverwrite = true)
{
try
{
if (gbEND_GENERAL == true)
return false;
System.IO.FileInfo objfileinfo =
new System.IO.FileInfo(sFolderFrom + sFileName + sExtension);
if (clsFolder.Exists(sFolderTo) == false)
{
clsFolder.Create(sFolderTo);
}
objfileinfo.CopyTo(sFolderTo + sFileName + sExtension, bOverwrite);
return true;
}
catch (System.Runtime.InteropServices.COMException ex)
{
mobjCOMException = ex;
return false;
}
catch (Exception ex)
{
mobjException = ex;
return false;
}
finally
{
if (gbDEBUG_GENERAL == true ||
(mobjCOMException != null || mobjException != null))
{
clsError.Handle(
"CopyTo",
msCLASSNAME,
"copy the following file:" +
gsCRLF + "'" + sFolderFrom + sFileName + sExtension + "'" +
gsCRLF + "to the folder and filename: " +
"'" + sFolderTo + sFileName + sExtension + "'",
mobjCOMException,
mobjException);
}
}
}
File_Delete
Removes a file. Add an optional confirmation prompt.public static void File_Delete(
string sFolderPath,
string sFileName = "",
string sExtension = "")
{
try
{
if (gbEND_GENERAL == true)
return;
System.IO.FileInfo objfileinfo =
new System.IO.FileInfo(sFolderPath + sFileName + sExtension);
objfileinfo.Delete();
}
catch (System.Runtime.InteropServices.COMException ex)
{
mobjCOMException = ex;
}
catch (Exception ex)
{
mobjException = ex;
}
finally
{
if (gbDEBUG_GENERAL == true ||
(mobjCOMException != null || mobjException != null))
{
clsError.Handle(
"Delete",
msCLASSNAME,
"delete the following file:" +
gsCRLF + "'" + sFolderPath + sFileName + sExtension + "'.",
mobjCOMException,
mobjException);
}
}
}
File_Exists
Checks if a file exists.public static bool File_Exists(
string sFolderPath,
string sFileName = "",
string sExtension = "",
bool bInformUser = false)
{
System.IO.FileInfo objFileInfo;
try
{
if (gbEND_GENERAL == true)
return false;
objFileInfo = new System.IO.FileInfo(sFolderPath + sFileName + sExtension);
bool result = objFileInfo.Exists;
if (result == false && bInformUser == true)
{
clszMessagesGeneral.FileDoesNotExistWarning(sFolderPath, sFileName);
}
return result;
}
catch (System.Runtime.InteropServices.COMException ex)
{
mobjCOMException = ex;
return false;
}
catch (Exception ex)
{
mobjException = ex;
return false;
}
finally
{
if (gbDEBUG_GENERAL == true ||
(mobjCOMException != null || mobjException != null))
{
clsError.Handle(
"Exists",
msCLASSNAME,
"determine if the following file exists:" +
gsCRLF + "'" + sFolderPath + sFileName + sExtension + "'.",
mobjCOMException,
mobjException);
}
}
}
File_LastWriteTimeGet
public static DateTime File_LastWriteTimeGet(
string sFolderPath,
string sFileName = "",
string sExtension = "")
{
try
{
if (gbEND_GENERAL == true)
return DateTime.MinValue;
System.IO.FileInfo objfileinfo =
new System.IO.FileInfo(sFolderPath + sFileName + sExtension);
return objfileinfo.LastWriteTime;
}
catch (System.Runtime.InteropServices.COMException ex)
{
mobjCOMException = ex;
return DateTime.MinValue;
}
catch (Exception ex)
{
mobjException = ex;
return DateTime.MinValue;
}
finally
{
if (gbDEBUG_GENERAL == true ||
(mobjCOMException != null || mobjException != null))
{
clsError.Handle(
"File_LastWriteTimeGet",
msCLASSNAME,
"return the 'LastWriteTime' for the following file:" +
gsCRLF +
"'" + sFolderPath + sFileName + sExtension + "'.",
mobjCOMException,
mobjException);
}
}
}
File_NameFromFullPath
public static string File_NameFromFullPath(
string sFullPath)
{
try
{
return System.IO.Path.GetFileName(sFullPath);
}
catch (System.Exception ex)
{
modMessages.ErrorMessage(System.Reflection.MethodBase.GetCurrentMethod(), ex.Message);
return "";
}
}
File_OpenDialog
Displays a dialog box to allow the user to open a file.public static string File_OpenDialog(
string sFilter,
int iFilterIndex,
string sInitialDirectory,
string sDefaultExtension,
bool bAddExtensionIfOmitted)
{
System.Windows.Forms.OpenFileDialog objFileDialog;
try
{
objFileDialog = new System.Windows.Forms.OpenFileDialog();
objFileDialog.CheckFileExists = true;
objFileDialog.CheckPathExists = true;
objFileDialog.ShowReadOnly = true;
objFileDialog.Filter = sFilter;
objFileDialog.FilterIndex = iFilterIndex;
objFileDialog.InitialDirectory = sInitialDirectory;
objFileDialog.DefaultExt = sDefaultExtension;
objFileDialog.AddExtension = bAddExtensionIfOmitted;
if (objFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
return objFileDialog.FileName;
}
return "";
}
catch (Exception ex)
{
modMessages.ErrorMessage(System.Reflection.MethodBase.GetCurrentMethod(), ex.Message);
return "";
}
}
File_ReadAllowed
public static bool File_ReadAllowed(
string sFolderPath,
string sFileName = "",
string sExtension = "")
{
System.IO.StreamReader objStreamReader;
string sfullpath;
bool ReadAllowed = false;
try
{
sfullpath = sFolderPath + sFileName + sExtension;
objStreamReader = new System.IO.StreamReader(
System.IO.File.OpenRead(sfullpath));
ReadAllowed = true;
}
catch (System.Runtime.InteropServices.COMException)
{
// VB swallowed this silently
}
catch (Exception)
{
// VB swallowed this silently
}
finally
{
if (ReadAllowed == false)
{
clszMessagesGeneral.FileBeingUsedByAnotherProcessWarning(sfullpath);
}
}
return ReadAllowed;
}
File_ReadCSVToArray
public static string[,] File_ReadCSVToArray(
string sFolderPath,
string sFileName = "",
string sExtension = ".csv")
{
try
{
if (gbEND_GENERAL == true)
return null;
System.Text.RegularExpressions.Regex regRegex;
string sfilecontents;
string slineoftext;
string[] saarraytemp = null;
sfilecontents = clsFile.ReadToString(sFolderPath, sFileName, sExtension);
regRegex = new System.Text.RegularExpressions.Regex("\r");
foreach (string line in regRegex.Split(sfilecontents))
{
slineoftext = line;
// only get the first line of data
saarraytemp = slineoftext.Split(',');
break;
}
// VB never returns anything — so C# must also return null
return null;
}
catch (System.Runtime.InteropServices.COMException ex)
{
mobjCOMException = ex;
return null;
}
catch (Exception ex)
{
mobjException = ex;
return null;
}
finally
{
if (gbDEBUG_GENERAL == true ||
(mobjCOMException != null || mobjException != null))
{
clsError.Handle(
"ReadCSVToArray",
msCLASSNAME,
".",
mobjCOMException,
mobjException);
}
}
}
File_ReadToString
public static string File_ReadToString(
string sFolderPath,
string sFileName = "",
string sExtension = "",
bool bCheckFileExists = true)
{
try
{
if (gbEND_GENERAL == true)
return null;
System.IO.StreamReader objStreamReader;
string sfullpath;
sfullpath = sFolderPath + sFileName + sExtension;
if (bCheckFileExists == true)
{
if (clsFile.Exists(sfullpath) == false)
{
clszMessagesGeneral.FileDoesNotExistWarning(sfullpath, "");
return "Invalid File";
}
}
objStreamReader = new System.IO.StreamReader(sfullpath);
string result = objStreamReader.ReadToEnd();
objStreamReader.Close();
return result;
}
catch (System.Runtime.InteropServices.COMException ex)
{
mobjCOMException = ex;
return null;
}
catch (Exception ex)
{
mobjException = ex;
return null;
}
finally
{
if (gbDEBUG_GENERAL == true ||
(mobjCOMException != null || mobjException != null))
{
clsError.Handle(
"ReadToString",
msCLASSNAME,
"return the contents of this file as a string.",
mobjCOMException,
mobjException);
}
}
}
File_SaveDialog
public static string File_SaveDialog(
string sFilter,
int iFilterIndex,
string sInitialDirectory = "",
bool bAddExtension = false,
string sDefaultExt = "",
bool bCheckFileExists = false,
bool bCheckPathExists = false)
{
System.Windows.Forms.SaveFileDialog objSaveFileDialog;
try
{
objSaveFileDialog = new System.Windows.Forms.SaveFileDialog();
objSaveFileDialog.Filter = sFilter;
objSaveFileDialog.InitialDirectory = sInitialDirectory;
objSaveFileDialog.CheckFileExists = bCheckFileExists;
objSaveFileDialog.CheckPathExists = bCheckPathExists;
objSaveFileDialog.AddExtension = bAddExtension;
objSaveFileDialog.DefaultExt = sDefaultExt;
objSaveFileDialog.FilterIndex = iFilterIndex;
if (objSaveFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
return objSaveFileDialog.FileName;
}
return "";
}
catch (System.Exception ex)
{
modMessages.ErrorMessage(System.Reflection.MethodBase.GetCurrentMethod(), ex.Message);
return "";
}
}
File_SizeReturn
public long File_SizeReturn(
string sFileFullPath)
{
System.IO.FileInfo objFileInfo;
try
{
if (My.Settings.USER_ERROR_OCCURRED == true)
return 0;
if (modGeneral.File_Exists(sFileFullPath, true))
{
objFileInfo = new System.IO.FileInfo(sFileFullPath);
return objFileInfo.Length;
}
else
{
return 0;
}
}
catch (System.Exception ex)
{
modMessages.ErrorMessage(System.Reflection.MethodBase.GetCurrentMethod(), ex.Message);
return 0;
}
}
File_ToByteArray
public byte[] File_ToByteArray(
string sFilePath)
{
System.IO.FileStream fStream;
byte[] fBinary;
try
{
fStream = new System.IO.FileStream(
sFilePath,
System.IO.FileMode.Open,
System.IO.FileAccess.Read,
System.IO.FileShare.ReadWrite);
fBinary = new byte[(int)fStream.Length];
fStream.Read(fBinary, 0, (int)fStream.Length);
fStream.Close();
return fBinary;
}
catch (Exception ex)
{
modMessages.ErrorMessage(System.Reflection.MethodBase.GetCurrentMethod(), ex.Message);
return null;
}
}
public byte[] File_ToByteArray2(string myFilePath)
{
byte[] objByteArray;
System.IO.FileStream objFileStream = null;
string sTemporaryFile;
int iDotPos;
try
{
iDotPos = Microsoft.VisualBasic.Strings.InStrRev(myFilePath, ".");
sTemporaryFile =
Microsoft.VisualBasic.Interaction.Environ("USERPROFILE") +
"\\DocUploadCopy." +
Microsoft.VisualBasic.Strings.Right(
myFilePath,
myFilePath.Length - iDotPos);
modLateBinding.File_Copy(myFilePath, sTemporaryFile);
objFileStream = new System.IO.FileStream(
sTemporaryFile,
System.IO.FileMode.Open,
System.IO.FileAccess.Read);
objByteArray = new byte[(int)objFileStream.Length];
objFileStream.Read(objByteArray, 0, (int)objFileStream.Length);
objFileStream.Close();
return objByteArray;
}
catch (System.Exception ex)
{
modMessages.ErrorMessage(System.Reflection.MethodBase.GetCurrentMethod(), ex.Message);
return null;
}
finally
{
objFileStream = null;
}
}
File_ToString
public string File_ToString(
string sFilePath)
{
System.IO.StreamReader objStreamReader;
try
{
objStreamReader = new System.IO.StreamReader(sFilePath);
return objStreamReader.ReadToEnd();
}
catch (Exception ex)
{
modMessages.ErrorMessage(System.Reflection.MethodBase.GetCurrentMethod(), ex.Message);
return "";
}
}
File_TypeReturn
public static string File_TypeReturn(
string sFileExtension)
{
try
{
if (gbEND_GENERAL == true)
return null;
switch (sFileExtension)
{
case ".bmp": return "Bitmap Image";
case ".css": return "CSS File";
case ".doc": return "Microsoft Word Document";
case ".dot": return "Microsoft Word Template";
case ".htm": return "HTML Document";
case ".ico": return "Icon";
case ".png": return "Macromedia Fireworks Doc";
case ".ppt": return "Microsoft PowerPoint Presentation";
case ".txt": return "Text Document";
case ".xla": return "Microsoft Excel Add-in";
case ".xls": return "Microsoft Excel Workbook";
case ".xlt": return "Microsoft Excel Template";
default: return "Unknown";
}
}
catch (System.Runtime.InteropServices.COMException ex)
{
mobjCOMException = ex;
return null;
}
catch (Exception ex)
{
mobjException = ex;
return null;
}
finally
{
if (gbDEBUG_GENERAL == true ||
(mobjCOMException != null || mobjException != null))
{
clsError.Handle(
"TypeReturn",
msCLASSNAME,
"",
mobjCOMException,
mobjException);
}
}
}
Folder_Accessible
public static bool Folder_Accessible(
string sFolderPath)
{
try
{
if (gbEND_GENERAL == true)
return false;
System.IO.DirectoryInfo objdirectoryinfo =
new System.IO.DirectoryInfo(sFolderPath);
bool Accessible = false;
if (objdirectoryinfo.GetDirectories().Length == 0 ||
objdirectoryinfo.GetDirectories().Length != 0)
{
Accessible = true;
}
return Accessible;
}
catch (System.Runtime.InteropServices.COMException)
{
return false;
}
catch (Exception)
{
return false;
}
}
Folder_BrowseReturn
public static void Folder_BrowseReturn(
string sNewFolderPath,
string sSearchPattern,
System.Windows.Forms.TextBox txtFolderPath,
System.Windows.Forms.ComboBox cboFileName)
{
try
{
if (gbEND_GENERAL == true)
return;
string[] safiles;
sNewFolderPath = clsFolder.LineAdd(sNewFolderPath);
txtFolderPath.Text = sNewFolderPath;
clsFolder.FilesToArraySingle(sNewFolderPath, "", out safiles, sSearchPattern, false);
if (safiles == null)
{
cboFileName.Items.Clear();
cboFileName.Items.Add("No files");
cboFileName.SelectedIndex = 0;
return;
}
else
{
clsArray.RemoveChars("array", safiles, sNewFolderPath.Length, 0);
clsArray.ToListComboBox(cboFileName, "", safiles);
clsFile.ListComboSelect(cboFileName);
}
}
catch (System.Runtime.InteropServices.COMException ex)
{
gobjCOMException = ex;
}
catch (Exception ex)
{
gobjException = ex;
}
finally
{
if (gbDEBUG_GENERAL == true ||
(gobjCOMException != null || gobjException != null))
{
clsError.Handle(
"BrowseReturn",
"clsFolder",
"transfer the files in the folder to the array ?",
gobjCOMException,
gobjException);
}
}
}
Folder_Create
Creates a folder. This will create sub folders if necessary as it starts at the top level.public bool Folder_Create(
string sFolderPath)
{
try
{
if (My.Settings.USER_ERROR_OCCURRED == true)
return false;
if (modGeneral.Folder_Exists(sFolderPath) == false)
{
System.IO.Directory.CreateDirectory(sFolderPath);
}
return true;
}
catch (System.Exception ex)
{
modMessages.ErrorMessage(System.Reflection.MethodBase.GetCurrentMethod(), ex.Message);
return false;
}
}
Folder_Exists
public static bool Folder_Exists(
string sFolderPath,
bool bInformUser = false)
{
System.IO.DirectoryInfo objDirectoryInfo;
try
{
if (gbEND_GENERAL == true)
return false;
objDirectoryInfo = new System.IO.DirectoryInfo(sFolderPath);
bool Exists = objDirectoryInfo.Exists;
if (Exists == false && bInformUser == true)
{
System.Windows.Forms.MessageBox.Show(
"The File or Folder does not exists : " +
Environment.NewLine +
"\"" + sFolderPath + "\"");
}
return Exists;
}
catch (System.Runtime.InteropServices.COMException ex)
{
gobjCOMException = ex;
return false;
}
catch (Exception ex)
{
gobjException = ex;
return false;
}
finally
{
if (gbDEBUG_GENERAL == true ||
(gobjCOMException != null || gobjException != null))
{
clsError.Handle(
"Exists",
"clsFolder",
"determine if this folder actually exists:" +
Environment.NewLine +
"'" + sFolderPath + "'." +
Environment.NewLine + Environment.NewLine +
"There may be an invalid character.",
gobjCOMException,
gobjException);
}
}
}
Folder_FilesHasAny
public static bool Folder_FilesHasAny(
string sFolderPath,
bool bCheckValid = true,
bool bCheckLineAdd = false,
string sSearchPattern = "")
{
try
{
if (gbEND_GENERAL == true)
return false;
System.IO.DirectoryInfo objfileinfo =
new System.IO.DirectoryInfo(sFolderPath);
bool FilesHasAny = false;
if (objfileinfo.GetFiles().Length == 0)
FilesHasAny = false;
if (objfileinfo.GetFiles().Length > 0)
FilesHasAny = true;
return FilesHasAny;
}
catch (System.Runtime.InteropServices.COMException ex)
{
gobjCOMException = ex;
return false;
}
catch (Exception ex)
{
gobjException = ex;
return false;
}
finally
{
if (gbDEBUG_GENERAL == true ||
(gobjCOMException != null || gobjException != null))
{
clsError.Handle(
"FilesHasAny",
"clsFolder",
"determine if there are any files in the folder path:" +
Environment.NewLine +
"'" + sFolderPath + "'.",
gobjCOMException,
gobjException);
}
}
}
Folder_FilesToArraySingle
Transfers all the files in a folder with a particular extension to a single dimensional array What is the difference between this and the above ?????.public static void Folder_FilesToArraySingle(
string sFolderPath,
string sArrayName,
ref string[] asArrayName,
string sSearchPattern = "*",
bool bInformUser = true,
bool bAddThreeDots = false)
{
try
{
if (gbEND_GENERAL == true)
return;
int itotalfiles;
int ifoldernumber;
string snextitem;
if (clsFolder.FilesHasAny(sFolderPath, false) == true)
{
asArrayName = System.IO.Directory.GetFiles(sFolderPath, sSearchPattern);
if (asArrayName.Length == 0)
asArrayName = null;
}
else
{
asArrayName = null;
if (bInformUser == true)
{
System.Windows.Forms.MessageBox.Show("There are no folders in this folder !");
}
}
}
catch (System.Runtime.InteropServices.COMException ex)
{
gobjCOMException = ex;
}
catch (Exception ex)
{
gobjException = ex;
}
finally
{
if (gbDEBUG_GENERAL == true ||
(gobjCOMException != null || gobjException != null))
{
clsError.Handle(
"FilesToArraySingle",
"clsFolder",
"transfer the list of all the files in the folder path:" +
Environment.NewLine +
"'" + sFolderPath + "'" +
"to the single dimensional array '" + sArrayName + "'",
gobjCOMException,
gobjException);
}
}
}
Folder_FilesToComboBox
public static void Folder_FilesToComboBox(
string sFolderPath,
System.Windows.Forms.ComboBox objComboBox,
string sSearchPattern = "*",
bool bInformUser = true,
bool bAddThreeDots = false)
{
try
{
if (gbEND_GENERAL == true)
return;
string[] asfiles;
int iarraycount;
if (clsFolder.FilesHasAny(sFolderPath, false) == true)
{
asfiles = System.IO.Directory.GetFiles(sFolderPath, sSearchPattern);
clsArray.RemoveChars("array", asfiles, sFolderPath.Length, 0);
for (iarraycount = 0; iarraycount <= asfiles.Length - 1; iarraycount++)
{
if (asfiles[iarraycount].ToString().Length > 0)
{
objComboBox.Items.Add(asfiles[iarraycount]);
}
}
}
else
{
if (bInformUser == true)
{
clszMessagesGeneral.FolderNoFilesInformation(sFolderPath);
}
}
}
catch (System.Runtime.InteropServices.COMException ex)
{
mobjCOMException = ex;
}
catch (Exception ex)
{
mobjException = ex;
}
finally
{
if (gbDEBUG_GENERAL == true ||
(mobjCOMException != null || mobjException != null))
{
clsError.Handle(
"FilesToComboBox",
msCLASSNAME,
"transfer the list of all the files in the folder path:" +
gsCRLF + "'" + sFolderPath + "'." +
"to the combo list box'" + "??" + "'",
mobjCOMException,
mobjException);
}
}
}
Folder_FilesToStr
Transfers all the files with a particular extension in a folder to a string concatenation.public static string Folder_FilesToStringConCat(
string sFolderPath,
string sSearchPattern = "*",
bool bInformUser = true,
string sSeperatorChar = ";")
{
try
{
if (gbEND_GENERAL == true)
return null;
string[] asfilesarray;
int itotalfiles;
int ifoldernumber;
string snextitem;
if (clsFolder.FilesHasAny(sFolderPath, false) == true)
{
asfilesarray = System.IO.Directory.GetFiles(sFolderPath, sSearchPattern);
if (asfilesarray.Length > 0)
{
return clsArray.ToStringConCat("FilesToString", asfilesarray, sSeperatorChar);
}
else
{
return "";
}
}
else
{
asfilesarray = null;
if (bInformUser == true)
{
System.Windows.Forms.MessageBox.Show("There are no folders in this folder !");
}
return null;
}
}
catch (System.Runtime.InteropServices.COMException ex)
{
gobjCOMException = ex;
return null;
}
catch (Exception ex)
{
gobjException = ex;
return null;
}
finally
{
if (gbDEBUG_GENERAL == true ||
(gobjCOMException != null || gobjException != null))
{
clsError.Handle(
"FilesToArraySingle",
"clsFolder",
"transfer the list of all the files in the folder path:" +
Environment.NewLine +
"'" + sFolderPath + "'" +
"to the string concatenation.",
gobjCOMException,
gobjException);
}
}
}
Folder_NetworkIsAvailable
public bool Folder_NetworkIsAvailable()
{
try
{
if (My.Settings.USER_ERROR_OCCURRED == true)
return false;
return System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable();
}
catch (System.Exception)
{
return false;
}
}
Folder_SubFoldersToArray
public static void Folder_SubFoldersToArray(
string sFolderPath,
string sArrayName,
ref string[] asFoldersArray,
bool bAddLine = true,
bool bRemoveHidden = true,
bool bInformUser = true)
{
try
{
if (gbEND_GENERAL == true)
return;
string[] asdirectories;
int itotalfiles;
int ifoldernumber;
string snextitem;
if (clsFolder.SubFoldersHasAny(sFolderPath, false) == true)
{
asdirectories = System.IO.Directory.GetDirectories(sFolderPath);
asFoldersArray = asdirectories;
if (bAddLine == true)
clsArray.AddAfter("\\", sArrayName, asFoldersArray);
if (bRemoveHidden == true)
clsArray.RemoveHiddenFolders(asFoldersArray);
}
else
{
if (bInformUser == true)
{
System.Windows.Forms.MessageBox.Show("There are no folders in this folder !");
}
}
}
catch (System.Runtime.InteropServices.COMException ex)
{
gobjCOMException = ex;
}
catch (Exception ex)
{
gobjException = ex;
}
finally
{
if (gbDEBUG_GENERAL == true ||
(gobjCOMException != null || gobjException != null))
{
clsError.Handle(
"SubFoldersToArray",
"clsFolder",
"transfer the list of sub folders in the folder path:" +
Environment.NewLine +
"'" + sFolderPath + "'." +
"to the single dimensional array '" + sArrayName + "'",
gobjCOMException,
gobjException);
}
}
}
Folder_TreeViewFolderSelect
public static void Folder_TreeViewFolderSelect(
ref bool gbAddFolders,
System.Windows.Forms.TreeView objTreeView,
string sNodePath,
System.Windows.Forms.TreeNodeCollection objTreeNodeColl)
{
try
{
if (gbEND_GENERAL == true)
return;
int inextlevel;
string snodetext;
System.Windows.Forms.TreeNode objnode;
if (Microsoft.VisualBasic.Strings.InStr(sNodePath, "\\") > 0)
{
snodetext = sNodePath.Substring(
0,
Microsoft.VisualBasic.Strings.InStr(sNodePath, "\\") - 1);
foreach (System.Windows.Forms.TreeNode node in objTreeNodeColl)
{
if (node.Text == snodetext)
{
gbAddFolders = false;
objTreeView.SelectedNode = node;
gbAddFolders = true;
clsTreeView.FoldersAdd(node);
clsTreeView.FolderSelect(
ref gbAddFolders,
objTreeView,
sNodePath.Substring(snodetext.Length + 1),
node.Nodes);
return;
}
}
}
else
{
foreach (System.Windows.Forms.TreeNode node in objTreeNodeColl)
{
if (node.Text == sNodePath)
{
gbAddFolders = false;
objTreeView.SelectedNode = node;
gbAddFolders = true;
return;
}
}
}
// subfolder not found — expand current selection
if (objTreeView.SelectedNode != null)
{
objTreeView.SelectedNode.Expand();
}
}
catch (System.Runtime.InteropServices.COMException ex)
{
gobjCOMException = ex;
}
catch (Exception ex)
{
gobjException = ex;
}
finally
{
if (gbDEBUG_GENERAL == true ||
(gobjCOMException != null || gobjException != null))
{
clsError.Handle(
"FolderSelect",
"clsTreeView",
"select this folder in the tree view.",
gobjCOMException,
gobjException);
}
}
}
FolderPath_LineAdd
public static string FolderPath_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;
}
}
FolderPath_LineRemove
public static string FolderPath_LineRemove(
string sFolderPath)
{
try
{
if (gbEND_GENERAL == true)
return null;
if (sFolderPath != "")
{
if (sFolderPath.Substring(sFolderPath.Length - 1, 1) == "\\")
{
return sFolderPath.Substring(0, sFolderPath.Length - 1);
}
else
{
return sFolderPath;
}
}
return null;
}
catch (System.Runtime.InteropServices.COMException ex)
{
gobjCOMException = ex;
return null;
}
catch (Exception ex)
{
gobjException = ex;
return null;
}
finally
{
if (gbDEBUG_GENERAL == true ||
(gobjCOMException != null || gobjException != null))
{
clsError.Handle(
"LineRemove",
"clsFolder",
"remove the character \"\\\" from the end of the folder path:" +
Environment.NewLine +
"'" + sFolderPath + "'.",
gobjCOMException,
gobjException);
}
}
}
Folders_ArrayRemoveHidden
public void Folders_ArrayRemoveHidden(
ref string[] asFoldersArray)
{
// VB's "On Error GoTo AnError" becomes a try/catch with a goto
try
{
int iarraycount;
int ifolderscount = 0;
string[] astemp;
int itempcount = 0;
for (iarraycount = 0; iarraycount <= asFoldersArray.Length - 1; iarraycount++)
{
System.IO.DirectoryInfo objfileinfo =
new System.IO.DirectoryInfo(asFoldersArray[iarraycount]);
if ((objfileinfo.Attributes & System.IO.FileAttributes.Hidden)
!= System.IO.FileAttributes.Hidden)
{
ifolderscount = ifolderscount + 1;
}
}
astemp = new string[ifolderscount - 1 + 1];
for (iarraycount = 0; iarraycount <= asFoldersArray.Length - 1; iarraycount++)
{
System.IO.DirectoryInfo objfileinfo =
new System.IO.DirectoryInfo(asFoldersArray[iarraycount]);
if ((objfileinfo.Attributes & System.IO.FileAttributes.Hidden)
!= System.IO.FileAttributes.Hidden)
{
astemp[itempcount] = asFoldersArray[iarraycount];
itempcount = itempcount + 1;
}
}
asFoldersArray = new string[astemp.Length];
asFoldersArray = astemp;
if (gbDEBUG == false)
return;
return; // fall through to avoid hitting the label
}
catch
{
// fall through to label
}
AnError:
Error_Handle("Folders_ArrayRemoveHidden", msMODULENAME,
"remove the hidden folders from the array");
}
Message_FileBeingUsedByAnotherProcess
public static void Message_FileBeingUsedByAnotherProcess(
string sFolderPath,
string sFileName = "",
string sExtension = "",
bool bFlagError = false)
{
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);
clsError.ErrorFlagChange(bFlagError);
}
Message_FileDoesNotExist
public static void Message_FileDoesNotExist(
string sFolderPath,
string sFileName = "",
string sExtension = "",
bool bFlagError = false)
{
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);
clsError.ErrorFlagChange(bFlagError);
}
Message_FileHasBeenSaved
public static void Message_FileHasBeenSaved(
string sFolderPath,
string sFileName = "",
string sExtension = "",
bool bFlagError = false)
{
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);
clsError.ErrorFlagChange(bFlagError);
}
Message_FolderDoesNotExist
public static void Message_FolderDoesNotExist(
string sFolderPath,
bool bFlagError = false)
{
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);
clsError.ErrorFlagChange(bFlagError);
}
Message_FolderNoFiles
public static void Message_FolderNoFiles(
string sFolderPath,
bool bFlagError = false)
{
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);
clsError.ErrorFlagChange(bFlagError);
}
Message_FolderNoSubFolders
public static void Message_FolderNoSubFolders(
string sFolderPath,
bool bFlagError = false)
{
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);
clsError.ErrorFlagChange(bFlagError);
}
Question_FileDelete
public static bool Question_FileDelete(
string sFolderPath,
string sFileName = "",
string sExtension = "",
int iDefaultButton = 2,
bool bFlagError = false)
{
bool breturn = false;
System.Windows.Forms.DialogResult objreturn;
System.Windows.Forms.MessageBoxDefaultButton objdefaultbutton =
System.Windows.Forms.MessageBoxDefaultButton.Button2;
switch (iDefaultButton)
{
case 1:
objdefaultbutton = System.Windows.Forms.MessageBoxDefaultButton.Button1;
break;
case 2:
objdefaultbutton = System.Windows.Forms.MessageBoxDefaultButton.Button2;
break;
}
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)
breturn = true;
if (objreturn == System.Windows.Forms.DialogResult.No)
breturn = false;
clsError.ErrorFlagChange(bFlagError);
return breturn;
}
Question_FileReplace
public static bool Question_FileReplace(
string sFolderPath,
string sFileName = "",
string sExtension = "",
int iDefaultButton = 2,
bool bFlagError = false)
{
bool breturn = false;
System.Windows.Forms.DialogResult objreturn;
System.Windows.Forms.MessageBoxDefaultButton objdefaultbutton =
System.Windows.Forms.MessageBoxDefaultButton.Button2;
switch (iDefaultButton)
{
case 1:
objdefaultbutton = System.Windows.Forms.MessageBoxDefaultButton.Button1;
break;
case 2:
objdefaultbutton = System.Windows.Forms.MessageBoxDefaultButton.Button2;
break;
}
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)
breturn = true;
if (objreturn == System.Windows.Forms.DialogResult.No)
breturn = false;
clsError.ErrorFlagChange(bFlagError);
return breturn;
}
Question_FolderDelete
public static bool Question_FolderDelete(
string sFoldersToDelete,
string sSeperatorChar = ";",
int iDefaultButton = 2,
bool bFlagError = false)
{
string sfoldertext;
bool breturn = false;
System.Windows.Forms.DialogResult objreturn;
System.Windows.Forms.MessageBoxDefaultButton objdefaultbutton =
System.Windows.Forms.MessageBoxDefaultButton.Button2;
if (sFoldersToDelete.IndexOf(sSeperatorChar) > 0)
{
sFoldersToDelete = sFoldersToDelete.Replace(
sSeperatorChar,
System.Environment.NewLine);
sfoldertext = "folders";
}
else
{
sfoldertext = "folder";
}
switch (iDefaultButton)
{
case 1:
objdefaultbutton = System.Windows.Forms.MessageBoxDefaultButton.Button1;
break;
case 2:
objdefaultbutton = System.Windows.Forms.MessageBoxDefaultButton.Button2;
break;
}
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)
breturn = true;
if (objreturn == System.Windows.Forms.DialogResult.No)
breturn = false;
clsError.ErrorFlagChange(bFlagError);
return breturn;
}
© 2026 Better Solutions Limited. All Rights Reserved. © 2026 Better Solutions Limited Top