video-preview/RyzStudio/IO/SmarterFileSystem.cs

282 lines
7.7 KiB
C#
Raw Permalink Normal View History

2021-10-03 17:43:59 +00:00
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace RyzStudio.IO
{
public class SmarterFileSystem
{
public static List<string> GetFiles(string path, string pattern, System.IO.SearchOption searchOption)
{
List<string> fileList = new List<string>();
List<string> directoryList = new List<string>();
directoryList.Add(path);
while (true)
{
if (directoryList.Count <= 0)
{
break;
}
string directory = directoryList.First();
directoryList.RemoveAt(0);
if (!IsDirectoryAccessible(directory))
{
continue;
}
IEnumerable<string> dirList = Enumerable.Empty<string>();
try
{
dirList = Directory.EnumerateDirectories(directory, "*", SearchOption.TopDirectoryOnly);
}
catch
{
continue;
}
foreach (string item in dirList)
{
//if (!IsDirectoryAccessible(item))
//{
// continue;
//}
directoryList.Add(item);
}
foreach (string item in Directory.EnumerateFiles(directory, "*", SearchOption.TopDirectoryOnly))
{
if (!IsFileAccessible(item))
{
continue;
}
fileList.Add(item);
}
}
return fileList;
}
//public static List<string> GetFiles(string path, string searchPattern, System.IO.SearchOption searchOption)
//{
// List<string> rs = new List<string>();
// if (string.IsNullOrWhiteSpace(path)) return rs;
// if (!System.IO.Directory.Exists(path)) return rs;
// IEnumerable<string> fileList = Enumerable.Empty<string>();
// //if (!IsDirectoryAccessible(path))
// //{
// // return rs;
// //}
// try
// {
// fileList = Directory.EnumerateFiles(path, "*", SearchOption.TopDirectoryOnly);
// }
// catch
// {
// return rs;
// }
// foreach (string item in fileList)
// {
// if (!IsFileAccessible(item))
// {
// continue;
// }
// rs.Add(item);
// }
// if (searchOption == SearchOption.AllDirectories)
// {
// foreach (string item in Directory.EnumerateDirectories(path, "*", SearchOption.TopDirectoryOnly))
// {
// //if (!IsDirectoryAccessible(item))
// //{
// // continue;
// //}
// rs.AddRange(GetFiles(item, searchPattern, searchOption));
// }
// }
// return rs;
//}
//public static string CleanFilename(string value, string defaultValue = "")
//{
// if (string.IsNullOrWhiteSpace(value))
// {
// return defaultValue;
// }
// else
// {
// return string.Join("-", value.Replace(".", "-").Split(Path.GetInvalidFileNameChars()));
// }
//}
//public static List<string> GetFiles(string path, string pattern)
//{
// List<string> fileList = new List<string>();
// List<string> directoryList = new List<string>();
// directoryList.Add(path);
// while (true)
// {
// if (directoryList.Count <= 0)
// {
// break;
// }
// string directory = directoryList.First();
// directoryList.RemoveAt(0);
// if (!IsDirectoryAccessible(directory))
// {
// continue;
// }
// foreach (string item in Directory.EnumerateDirectories(directory, "*", SearchOption.TopDirectoryOnly))
// {
// if (!IsDirectoryAccessible(item))
// {
// continue;
// }
// directoryList.Add(item);
// }
// foreach (string item in Directory.EnumerateFiles(directory, pattern, SearchOption.TopDirectoryOnly))
// {
// if (!IsFileAccessible(item))
// {
// continue;
// }
// fileList.Add(item);
// }
// }
// return fileList;
//}
public static bool IsFileAccessible(string filename)
{
if (string.IsNullOrWhiteSpace(filename)) return false;
if (!System.IO.File.Exists(filename)) return false;
try
{
System.IO.File.GetAttributes(filename);
}
catch
{
return false;
}
return true;
}
public static bool IsDirectoryAccessible(string path)
{
if (string.IsNullOrWhiteSpace(path)) return false;
if (!System.IO.Directory.Exists(path)) return false;
//try
//{
// System.IO.Directory.GetCreationTime(path);
//}
//catch
//{
// return false;
//}
return true;
}
//public static string ResolvePath(string value)
//{
// string rv = Environment.ExpandEnvironmentVariables(value);
// rv = resolveFirstPath(rv);
// rv = resolveLastPath(rv);
// return rv;
//}
//protected static string resolveFirstPath(string value)
//{
// const string last = "%FIRST%";
// if (!value.Contains(last))
// {
// return value;
// }
// string head = value.Substring(0, value.IndexOf(last));
// string tail = value.Substring(value.IndexOf(last) + last.Length);
// string[] dirList = new string[0];
// try
// {
// dirList = System.IO.Directory.GetDirectories(head, "*", System.IO.SearchOption.TopDirectoryOnly);
// }
// catch
// {
// // do nothing
// }
// if (dirList.Length <= 0)
// {
// return value;
// }
// return dirList[0] + tail;
//}
//protected static string resolveLastPath(string value)
//{
// const string last = "%LAST%";
// if (!value.Contains(last))
// {
// return value;
// }
// string head = value.Substring(0, value.IndexOf(last));
// string tail = value.Substring(value.IndexOf(last) + last.Length);
// string[] dirList = new string[0];
// try
// {
// dirList = System.IO.Directory.GetDirectories(head, "*", System.IO.SearchOption.TopDirectoryOnly);
// }
// catch
// {
// // do nothing
// }
// if (dirList.Length <= 0)
// {
// return value;
// }
// return dirList[(dirList.Length - 1)] + tail;
//}
}
}