Jump to content

(C#) Filesystem snippets


Recommended Posts

Posted

I created a DLL for myself with usefull methods that have to do with the windows filesystem, decided to just share it here

 

Files

/// <summary>
    /// This class contains usefull methods that provide info about files
    /// </summary>
    public static class Files
    {
        
 
      
        
 
        static OpenFileDialog fileDialog = new OpenFileDialog();
        /// <summary>
        /// Gets all the selected files and their path in the folder with an OpenFileDialog.
        /// </summary>
        /// <returns>string array of files and their path</returns>
        public static string[] getSelectedFilesWithPath()
        {            
            fileDialog.Multiselect = true;
            if (fileDialog.ShowDialog() == DialogResult.OK)
            {
                string[] files = new string[fileDialog.FileNames.Length];
                for (int i = 0; i < files.Length; i++)
                {
                    files[i] = fileDialog.FileNames[i];
                }
                return files;                
            }
            else
                return new string[] { "" };
        }
 
        /// <summary>
        /// Gets all the selected files and their path in the folder with an OpenFileDialog.
        /// </summary>
        /// <param name="filterDescription">Description of the filter. Example: Image files</param>
        /// <param name="extensionFilter">File extension filter. Example: "*.jpg; *.jpeg;"</param>
        /// <returns>string array of files and their path</returns>
        public static string[] getSelectedFilesWithPath(string filterDescription, string extensionFilter)
        {            
            fileDialog.Multiselect = true;
            fileDialog.Filter = (filterDescription + "| " + extensionFilter);
            if (fileDialog.ShowDialog() == DialogResult.OK)
            {
                string[] files = new string[fileDialog.FileNames.Length];
                for (int i = 0; i < files.Length; i++)
                {
                    files[i] = fileDialog.FileNames[i];
                }
                return files;
            }
            else
                return new string[] { "" };
        }
 
 
        /// <summary>
        /// Gets all names and extensions from the selected files with an OpenFileDialog.
        /// </summary>
        /// <returns>string array of files and their extensions</returns>
        public static string[] getSelectedFileNames()
        {                        
            fileDialog.Multiselect = true;
            if (fileDialog.ShowDialog() == DialogResult.OK)
            {
                string[] files = new string[fileDialog.FileNames.Length];
                for (int i = 0; i < fileDialog.SafeFileNames.Length; i++)
                {
                    files[i] = fileDialog.SafeFileNames[i];
                }
                return files;
            }
            else
                return new string[] { "" };
        }
 
 
        /// <summary>
        /// Gets the name and the extension of the selected file
        /// </summary>
        /// <returns>The name of the selected file and its extension</returns>
        public static string getSelectedFileName()
        {
            fileDialog.Multiselect = false;
            if (fileDialog.ShowDialog() == DialogResult.OK)
            {
                return fileDialog.SafeFileName;
            }
            else
                return "";
        }
 
        /// <summary>
        /// Gets the path to the selected file
        /// </summary>
        /// <returns>Path to the file and its name and extension</returns>
        public static string getSelectedFileWithPath()
        {
            fileDialog.Multiselect = false;
            if (fileDialog.ShowDialog() == DialogResult.OK)
            {
                return fileDialog.FileName;
            }
            else
                return "";
        }
 
       
 
        /// <summary>
        /// Gets all the files and the path to them in the given path 
        /// </summary>
        /// <param name="path">Path of the folder</param>
        /// <returns>string array of files and their path</returns>
        public static string[] GetFilesWithPathInFolder(string path)
        {
            return Directory.GetFiles(path);            
        }
 
        /// <summary>
        /// Gets all the files and the path to them in the given path 
        /// </summary>
        /// <param name="path">Path of the folder</param>
        /// <param name="filter">File filter. Example: "*.jpg; *.jpeg;"</param>
        /// <returns>string array of files and their path</returns>
        public static string[] getFilesWithPathInFolder(string path,string filter)
        {
 
            string supportedExtensions = filter;
            List<string> files = new List<string>();
            foreach (string file in Directory.GetFiles(path, "*.*", SearchOption.AllDirectories).Where(s => supportedExtensions.Contains(Path.GetExtension(s).ToLower())))
            {
                files.Add(file);
            }
            return files.ToArray();
        }
 
        /// <summary>
        /// Gets all the filenames and their extension in the given path 
        /// </summary>
        /// <param name="path">Path of the folder</param>
        /// <returns>string array of filenames and their extensions</returns>
        public static string[] getFileNamesInFolder(string path)
        {
            string[] fullFiles =  Directory.GetFiles(path);
            List<string> fileNames = new List<string>();
            foreach(string file in fullFiles)
            {                
                fileNames.Add(Path.GetFileName(file));
            }
 
            return fileNames.ToArray();
        }
 
 
        /// <summary>
        /// Gets all the filenames and their extension in the given path 
        /// </summary>
        /// <param name="path">Path of the folder</param>
        /// <returns>string array of filenames and their extensions</returns>
        public static string[] getFileNamesInFolder(string path,string filter)
        {            
            string supportedExtensions = filter;
            List<string> files = new List<string>();
            foreach (string file in Directory.GetFiles(path, "*.*", SearchOption.AllDirectories).Where(s => supportedExtensions.Contains(Path.GetExtension(s).ToLower())))
            {
                files.Add(file);
            }
            
            List<string> fileNames = new List<string>();
            foreach (string file in files)
            {
                fileNames.Add(Path.GetFileName(file));
            }
 
            return fileNames.ToArray();
        }
 
        /// <summary>
        /// Gets the size of a file in bytes
        /// </summary>
        /// <param name="path">Path of the file</param>
        /// <returns>Size of the file in bytes</returns>
        public static long getFileSize(string path)
        {
            FileInfo finf = new FileInfo(path);
            return finf.Length;
        }
 
        public static void CreateHiddenFile(string path)
        {
            FileInfo fInfo = new FileInfo(path);
            fInfo.Create();
            fInfo.Attributes |= FileAttributes.Hidden;
        }
 
 
 
 
 
 
        /// <summary>
        /// Class for file attributes
        /// </summary>
        public class Properties
        {
            public static bool isReadOnly(string path)
            {
                FileInfo fInfo = new FileInfo(path);
 
                if (fInfo.IsReadOnly)
                    return true;
                else
                    return false;                
            }
            public static bool ishidden(string path)
            {
                FileInfo fInfo = new FileInfo(path);
 
                if (fInfo.Attributes.HasFlag(FileAttributes.Hidden))
                    return true;
                else
                    return false;
            }
            public static bool isEncrypted(string path)
            {
                FileInfo fInfo = new FileInfo(path);
                
                if (fInfo.Attributes.HasFlag(FileAttributes.Encrypted))
                    return true;
                else
                    return false;
            }
 
            public static void setReadOnly(string path, bool yes)
            {
                var attr = File.GetAttributes(path);
                if (yes)
                {
                    attr = attr | FileAttributes.ReadOnly;
                    File.SetAttributes(path, attr);
                }
                else
                {
                    attr = attr & ~FileAttributes.ReadOnly;
                    File.SetAttributes(path, attr);
                }
            }
            public static void setHidden(string path, bool yes)
            {
                var attr = File.GetAttributes(path);
                if (yes)
                {
                    attr = attr | FileAttributes.Hidden;
                    File.SetAttributes(path, attr);
                }
                else
                {
                    attr = attr & ~FileAttributes.Hidden;
                    File.SetAttributes(path, attr);
                }
            }
            public static void setEncrypted(string path, bool yes)
            {
                var attr = File.GetAttributes(path);
                if (yes)
                {
                    File.Encrypt(path);                    
                }
                else
                {
                    File.Decrypt(path);
                }
            }
        }
 
    }

 

Folders

/// <summary>
    /// This class contains usefull methods that provide info about folders
    /// </summary>
    public class Folders
    {
        /// <summary>
        /// Gets all the files in the folder and calculates the total file size in bytes
        /// </summary>
        /// <param name="path">Path of the folder</param>
        /// <returns>Total file size in bytes</returns>
        public static long getTotalFileSizeInFolder(string path)
        {
            long totalSize = 0;
            string[] files = Files.GetFilesWithPathInFolder(path);
            for (int i = 0; i < files.Length; i++)
                totalSize = totalSize + Files.getFileSize(files[i]);
 
            return totalSize;
        }
 
        /// <summary>
        /// Deletes all files in the folder
        /// </summary>
        /// <param name="path">Path to the folder</param>
        public static void deleteFilesInFolder(string path)
        {
            string[] files = Directory.GetFiles(path);
 
            foreach(string t in files)
            {
                File.Delete(t);
            }
        }
 
        /// <summary>
        /// Deletes every file in a folder that matches the filter
        /// </summary>
        /// <param name="path">Path to the folder</param>
        /// <param name="filter">Extension filter. Example: "*.jpg; *.jpeg;"</param>
        public static void deleteFilesInFolder(string path,string filter)
        {                       
            string supportedExtensions = filter;
            List<string> files = new List<string>();
            foreach (string file in Directory.GetFiles(path, "*.*", SearchOption.AllDirectories).Where(s => supportedExtensions.Contains(Path.GetExtension(s).ToLower())))
            {
                File.Delete(file);
            }
        }
        public static void CreateHiddenFolder(string path)
        {
            DirectoryInfo dInfo = new DirectoryInfo(path);
            dInfo.Create();
            dInfo.Attributes |= FileAttributes.Hidden;
        }
 
 
 
 
 
 
 
        /// <summary>
        /// Class for file attributes
        /// </summary>
        public class Properties
        {
            public static bool isReadOnly(string path)
            {                
                DirectoryInfo dInfo = new DirectoryInfo(path);
 
                if (dInfo.Attributes.HasFlag(FileAttributes.ReadOnly))
                    return true;
                else
                    return false;
            }
            public static bool ishidden(string path)
            {
                DirectoryInfo dInfo = new DirectoryInfo(path);
 
                if (dInfo.Attributes.HasFlag(FileAttributes.Hidden))
                    return true;
                else
                    return false;
            }
 
            public static void setReadOnly(string path, bool yes)
            {                
                var attr = File.GetAttributes(path);
                if (yes)
                {
                    attr = attr | FileAttributes.ReadOnly;
                    File.SetAttributes(path, attr);
                }
                else
                {
                    attr = attr & ~FileAttributes.ReadOnly;
                    File.SetAttributes(path, attr);
                }
            }
            public static void setHidden(string path, bool yes)
            {
                var attr = File.GetAttributes(path);
                if (yes)
                {
                    attr = attr | FileAttributes.Hidden;
                    File.SetAttributes(path, attr);
                }
                else
                {
                    attr = attr & ~FileAttributes.Hidden;
                    File.SetAttributes(path, attr);
                }
            }
            public static void setEncrypted(string path, bool yes)
            {
                var attr = File.GetAttributes(path);
                if (yes)
                {
                    File.Encrypt(path);
                }
                else
                {
                    File.Decrypt(path);
                }
            }
        }
    }

 

Shortcuts

public class Shortcuts
    {
        static IWshShortcut Shortcut;
        static WshShell shell = new WshShell();
 
        /// <summary>
        /// Creates a shortcut of a file
        /// </summary>
        /// <param name="shortcutPath">Path of the shortcut. Where do you want to place the shortcut?</param>
        /// <param name="shortcutName">The name of the shortcut that will appear in the windows file explorer</param>
        /// <param name="targetPath">Path to the target of the shortcut. What does the shortcut target?</param>
        /// <param name="description">Description of the shortcut.</param>
        public static void CreateShortcut(string shortcutPath, string shortcutName, string targetPath, string description)
        {                                               
            //Where do you want to create the shortcut + what is the name of the shortcut?
            Shortcut = (IWshShortcut)shell.CreateShortcut(shortcutPath + "\\" + shortcutName + ".lnk");
            //What is the shortcut's target?
            Shortcut.TargetPath = targetPath;
            //Tooltip
            Shortcut.Description = description;
            Shortcut.Save();            
        }        
    }

 

 

 

Sample usage

FSManager.Folders.CreateHiddenFolder(@"C:\test");
 
 
FSManager.Folders.deleteFilesInFolder(@"C:\users\me\documents\sample folder", "*.png;*.bmp;*.jpg;*.jpeg"); //deletes image files in sample folder
 
 
List<string> fileNames = FSManager.Files.getFileNamesInFolder("C:\test").ToList();
string[] fileNames = FSManager.Files.getFileNamesInFolder("C:\test");

 

 

 

DLL: https://mega.nz/#!tAQSEKgA!u6eCjqJlf59EM3zyLVI1id4vnIwPa3jg2sDy7U4HRjc

  • Like 3

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...