Пример #1
0
        public static List <FileModel> GetDrives()
        {
            List <FileModel> drives = new List <FileModel>();

            try
            {
                foreach (string drive in Directory.GetLogicalDrives())
                {
                    DriveInfo dInfo = new DriveInfo(drive);

                    FileModel dModel = new FileModel()
                    {
                        Icon         = IconHelpers.GetIconOfFile(drive, true, true),
                        Name         = dInfo.Name,
                        Path         = dInfo.Name,
                        DateModified = DateTime.Now,
                        Type         = FileType.Drive,
                        SizeBytes    = dInfo.TotalSize
                    };
                    drives.Add(dModel);
                }
                return(drives);
            }
            catch (IOException io)
            {
                MessageBox.Show($"IO Exception getting drives: {io.Message}", "Exception getting drives");
            }
            catch (UnauthorizedAccessException noAccess)
            {
                MessageBox.Show($"No access for a hard drive: {noAccess.Message}", "");
            }
            return(drives);
        }
Пример #2
0
        public static List <FileModel> GetFiles(string directory)
        {
            List <FileModel> files = new List <FileModel>();

            if (!directory.IsDirectory())
            {
                return(files);
            }
            string currentFile = "";

            try
            {
                foreach (string file in Directory.GetFiles(directory))
                {
                    currentFile = file;
                    if (Path.GetExtension(file) != ".lnk")
                    {
                        FileInfo  fInfo  = new FileInfo(file);
                        FileModel fModel = new FileModel()
                        {
                            Icon         = IconHelpers.GetIconOfFile(file, true, false),
                            Name         = fInfo.Name,
                            Path         = fInfo.FullName,
                            DateCreated  = fInfo.CreationTime,
                            DateModified = fInfo.LastWriteTime,
                            Type         = FileType.File,
                            SizeBytes    = fInfo.Length
                        };
                        files.Add(fModel);
                    }
                }
                return(files);
            }

            catch (IOException io)
            {
                MessageBox.Show(
                    $"IO Exception getting files in directory: {io.Message}",
                    "Exception getting files in directory");
            }
            catch (UnauthorizedAccessException noAccess)
            {
                MessageBox.Show(
                    $"No access for a file: {noAccess.Message}",
                    "Exception getting files in directory");
            }
            catch (Exception e)
            {
                MessageBox.Show(
                    $"Failed to get files in '{directory}' || " +
                    $"Something to do with '{currentFile}'\n" +
                    $"Exception: {e.Message}", "Error");
            }

            return(files);
        }
Пример #3
0
        public async void TryNavigateToPath(string path)
        {
            if (path == string.Empty)
            {
                ClearFiles();

                foreach (FileModel drive in Fetcher.GetDrives())
                {
                    FilesControl fc = CreateFileControl(drive);
                    AddFile(fc);
                }
            }
            else if (path.IsFile())
            {
                MessageBox.Show($"Opening {path}");
            }
            else if (path.IsDirectory())
            {
                ClearFiles();
                string backPath = path.Substring(0, path.LastIndexOf('\\'));
                if (!backPath.Contains('\\'))
                {
                    backPath += "\\";
                    DriveInfo dInfo  = new DriveInfo(backPath);
                    FileModel dModel = new FileModel()
                    {
                        Icon         = IconHelpers.GetIconOfFile(backPath, true, true),
                        Name         = dInfo.Name,
                        Path         = dInfo.Name,
                        DateModified = DateTime.Now,
                        Type         = FileType.Drive,
                        SizeBytes    = dInfo.TotalSize
                    };
                    FilesControl back = CreateFileControl(dModel);
                    AddFile(back);
                }
                else
                {
                    DirectoryInfo dInfo  = new DirectoryInfo(backPath);
                    FileModel     dModel = new FileModel()
                    {
                        Icon         = IconHelpers.GetIconOfFile(backPath, true, true),
                        Name         = dInfo.Name,
                        Path         = dInfo.FullName,
                        DateCreated  = dInfo.CreationTime,
                        DateModified = dInfo.LastWriteTime,
                        Type         = FileType.Folder,
                        SizeBytes    = Fetcher.DirSize(new DirectoryInfo(backPath), 1)
                    };
                    FilesControl back = CreateFileControl(dModel);
                    AddFile(back);
                }
                List <FileModel> directories = new List <FileModel>();
                await Fetcher.GetDirectories(path, directories);

                foreach (FileModel dir in directories)
                {
                    FilesControl fc = CreateFileControl(dir);
                    AddFile(fc);
                }

                foreach (FileModel file in Fetcher.GetFiles(path))
                {
                    FilesControl fc = CreateFileControl(file);
                    AddFile(fc);
                }
            }
            else
            {
                MessageBox.Show("Something get wrong");
            }
        }
Пример #4
0
        public static async Task GetDirectories(string directory, List <FileModel> directories)
        {
            await Task.Run(() =>
            {
                if (!directory.IsDirectory())
                {
                    return(directories);
                }
                string currentDirectory = "";
                try
                {
                    foreach (string dir in Directory.GetDirectories(directory))
                    {
                        currentDirectory = dir;

                        DirectoryInfo dInfo = new DirectoryInfo(dir);
                        FileModel dModel    = new FileModel()
                        {
                            Icon         = IconHelpers.GetIconOfFile(dir, true, true),
                            Name         = dInfo.Name,
                            Path         = dInfo.FullName,
                            DateCreated  = dInfo.CreationTime,
                            DateModified = dInfo.LastWriteTime,
                            Type         = FileType.Folder,
                            SizeBytes    = DirSize(new DirectoryInfo(dir), 1)
                        };

                        directories.Add(dModel);
                    }

                    foreach (string file in Directory.GetFiles(directory))
                    {
                        if (Path.GetExtension(file) == ".lnk")
                        {
                            string realDirPath = ExplorerHelper.GetShortcutTargetFolder(file);
                            FileInfo dInfo     = new FileInfo(realDirPath);
                            FileModel dModel   = new FileModel()
                            {
                                Icon         = IconHelpers.GetIconOfFile(realDirPath, true, true),
                                Name         = dInfo.Name,
                                Path         = dInfo.FullName,
                                DateCreated  = dInfo.CreationTime,
                                DateModified = dInfo.LastWriteTime,
                                Type         = FileType.File,
                                SizeBytes    = 0
                            };

                            directories.Add(dModel);
                        }
                    }
                    return(directories);
                }
                catch (IOException io)
                {
                    MessageBox.Show(
                        $"IO Exception getting folders in directory: {io.Message}",
                        "Exception getting folders in directory");
                }
                catch (UnauthorizedAccessException noAccess)
                {
                    MessageBox.Show(
                        $"No access for a directory: {noAccess.Message}",
                        "Exception getting folders in directory");
                }
                catch (Exception e)
                {
                    MessageBox.Show(
                        $"Failed to get directories in '{directory}' || " +
                        $"Something to do with '{currentDirectory}'\n" +
                        $"Exception: {e.Message}", "Error");
                }
                return(directories);
            });
        }