本文整理汇总了C#中SearchOption类的典型用法代码示例。如果您正苦于以下问题:C# SearchOption类的具体用法?C# SearchOption怎么用?C# SearchOption使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
SearchOption类属于命名空间,在下文中一共展示了SearchOption类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetDirectoryFiles
/// <summary>
/// A safe way to get all the files in a directory and sub directory without crashing on UnauthorizedException
/// </summary>
/// <param name="rootPath">Starting directory</param>
/// <param name="patternMatch">Filename pattern match</param>
/// <param name="searchOption">Search subdirectories or only top level directory for files</param>
/// <returns>List of files</returns>
public static IEnumerable<string> GetDirectoryFiles(string rootPath, string patternMatch, SearchOption searchOption)
{
IEnumerable<string> foundFiles = Enumerable.Empty<string>(); // Start with an empty container
if (searchOption == SearchOption.AllDirectories)
{
try
{
IEnumerable<string> subDirs = Directory.EnumerateDirectories(rootPath);
foreach (string dir in subDirs)
{
try
{
foundFiles = foundFiles.Concat(GetDirectoryFiles(dir, patternMatch, searchOption)); // Add files in subdirectories recursively to the list
}
catch (UnauthorizedAccessException) { } // Incase we have an access error - we don't want to mask the rest
}
}
catch (UnauthorizedAccessException) { } // Incase we have an access error - we don't want to mask the rest
}
try
{
foundFiles = foundFiles.Concat(Directory.EnumerateFiles(rootPath, patternMatch)); // Add files from the current directory to the list
}
catch (UnauthorizedAccessException) { } // Incase we have an access error - we don't want to mask the rest
return foundFiles; // This is it finally
}
示例2: Search
public ActionResult Search(SearchOption searchOption, string searchText)
{
switch ((int)searchOption)
{
case 0:
// search by Title Name
return RedirectToAction("/GetTitlesByName", new { titleName = searchText });
break;
case 1:
// search by Genre
return RedirectToAction("/GetTitlesByGenre", new { genre = searchText });
break;
case 2:
// search by Language
return RedirectToAction("/GetTitlesByLanguage", new { language = searchText });
break;
case 3:
// search by Participant
return RedirectToAction("/GetTitlesByParticipant", new { participant = searchText });
break;
default:
// All
return RedirectToAction("/GetAllTitles/");
break;
}
}
示例3: GetDirectories
public static IEnumerable<string> GetDirectories(string path, SearchOption option)
{
IEnumerable<string> dirs = null;
try
{
dirs = Directory.EnumerateDirectories(path);
//dirs = Directory.GetDirectories(path).ToList();
}
catch (UnauthorizedAccessException)
{
}
if (dirs != null)
{
foreach (var dir in dirs)
{
if (option == SearchOption.AllDirectories)
{
foreach (var x in GetDirectories(dir, option))
{
yield return x;
}
}
yield return dir;
}
}
}
示例4: GetFiles
public static String[] GetFiles(
string folder,
string jobExtension,
SearchOption searchOption)
{
return MyDirectory.GetFiles(folder, jobExtension, searchOption);
}
示例5: GetFileFullNameListByKeyword
//根据关键字获得文件全名列表
public static string GetFileFullNameListByKeyword(string strFolderPath, string strKeyword, string searchPattern, SearchOption searchOption)
{
string strResult = "";
DirectoryInfo directoryInfo = new DirectoryInfo(strFolderPath);
//按文件名称升序排列
FileInfo[] fileInfos = directoryInfo.GetFiles(searchPattern, searchOption).OrderBy(p => p.Name).ToArray();
string strContent = "";
for (int i = 0; i < fileInfos.Length; i++)
{
StreamReader sr = new StreamReader(fileInfos[i].OpenRead(), Encoding.Default);
strContent = sr.ReadToEnd();
if (strContent.Contains(strKeyword))
{
strResult += fileInfos[i].FullName + "|";
}
sr.Close();
sr.Dispose();
strContent = "";
}
Array.Clear(fileInfos, 0, fileInfos.Length);
return strResult;
}
示例6: LoadImages
public static List<ImageDescriptor> LoadImages(Uri uri, SearchOption searchOption)
{
var folderPath = uri.OriginalString;
var fullNames = new string[] { };
if (!folderPath.Equals(string.Empty))
fullNames = Directory.GetFiles(folderPath, "*.*", searchOption);
var list = new List<ImageDescriptor>();
for (var i = 0; i < fullNames.Length; i++)
{
var filePath = fullNames[i];
var imgInfo = DecodeBitmapImage(ref filePath, 128);
if (imgInfo != null)
list.Add(imgInfo);
}
GC.Collect();
return list;
}
示例7: FileSearchTemplate
public FileSearchTemplate(DirectoryInfo directory, string pattern, SearchOption option)
: this()
{
Pattern = pattern;
Directory = directory;
SearchOption = option;
}
示例8: SearchData
/// <summary>
/// Initializes a new instance of the <see cref="SearchData"/> class.
/// </summary>
/// <param name="fullPath">The full path.</param>
/// <param name="userPath">The user path.</param>
/// <param name="searchOptions">The search options.</param>
public SearchData(String fullPath, String userPath, SearchOption searchOptions)
{
Contract.Requires(fullPath != null);
Contract.Requires(searchOptions == SearchOption.AllDirectories || searchOptions == SearchOption.TopDirectoryOnly);
if( PathHelperMethods.IsDirectorySeparator(fullPath[fullPath.Length - 1]) )
{
this.fullPath = fullPath;
}
else
{
this.fullPath = fullPath + Path.DirectorySeparatorChar;
}
if( string.IsNullOrEmpty(userPath) || PathHelperMethods.IsDirectorySeparator(userPath[userPath.Length - 1]) )
{
this.userPath = userPath;
}
else
{
this.userPath = userPath + Path.DirectorySeparatorChar;
}
this.searchOptions = searchOptions;
}
示例9: CopyTo
public static void CopyTo(this DirectoryInfo sourceDirectory, string target, SearchOption option, string searchPattern)
{
if (target[target.Length - 1] != Path.DirectorySeparatorChar)
{
target += Path.DirectorySeparatorChar;
}
if (!Directory.Exists(target))
{
Directory.CreateDirectory(target);
for (int i = 0; i < searchPattern.Length; i++)
{
foreach (string file in Directory.GetFiles(sourceDirectory.FullName, searchPattern))
{
Console.WriteLine("Copying {0} to {1}", file, target + Path.GetFileName(file));
File.Copy(file, target + Path.GetFileName(file), true);
}
}
// Copy subdirectories (recursively)
if (option == SearchOption.AllDirectories)
{
foreach (string element in Directory.GetDirectories(sourceDirectory.FullName))
{
DirectoryInfo d = new DirectoryInfo(element);
CopyTo(d, target + Path.GetFileName(element), option, searchPattern);
}
}
}
}
示例10: FindFiles
/// <summary>
/// Static method to find files in a specified path.
/// </summary>
/// <param name="path">Path to the folder where to look for files.</param>
/// <param name="pattern">File name pPattern for the search.</param>
/// <param name="so">Search option. TopDirectoryOnly to search only in the top folder or AllDirectories to search also in the sub-directories. The default is TopDirectoryOnly.</param>
/// <returns></returns>
public static FileInfo[] FindFiles(string path, string pattern, SearchOption so = SearchOption.TopDirectoryOnly)
{
DirectoryInfo di = new System.IO.DirectoryInfo(path);
FileInfo[] fl = di.GetFiles(pattern, so);
return fl;
}
示例11: EnumerateFiles
/// <summary>
/// Returns an enumerable collection of files in a specified path.
/// </summary>
/// <param name="pattern">Search pattern. Uses Win32 native filtering.</param>
/// <param name="searchOption">Specifiy depth with <see cref="SearchOption"/></param>
/// <param name="enumerateOptions">Options <see cref="QuickIOEnumerateOptions"/></param>
/// <returns>An enumerable collection of the full names (including paths) for the files in the directory specified by path.</returns>
public IEnumerable<QuickIOFileInfo> EnumerateFiles( String pattern = QuickIOPatterns.PathMatchAll, SearchOption searchOption = SearchOption.TopDirectoryOnly, QuickIOEnumerateOptions enumerateOptions = QuickIOEnumerateOptions.None )
{
Contract.Requires( !String.IsNullOrWhiteSpace( pattern ) );
Contract.Ensures(Contract.Result<IEnumerable<QuickIOFileInfo>>() != null);
return QuickIODirectory.EnumerateFiles( FullNameUnc, pattern, searchOption );
}
示例12: EnumerateFilePaths
/// <summary>
/// Returns an enumerable collection of file names in a specified path.
/// </summary>
/// <param name="pattern">Search pattern. Uses Win32 native filtering.</param>
/// <param name="searchOption">Specifiy depth with <see cref="SearchOption"/></param>
/// <param name="pathFormatReturn">Specifies the type of path to return.</param>
/// <param name="enumerateOptions">Options</param>
/// <returns>An enumerable collection of the full names (including paths) for the files in the directory specified by path.</returns>
public IEnumerable<string> EnumerateFilePaths( String pattern = QuickIOPatterns.PathMatchAll, SearchOption searchOption = SearchOption.TopDirectoryOnly, QuickIOPathType pathFormatReturn = QuickIOPathType.Regular, QuickIOEnumerateOptions enumerateOptions = QuickIOEnumerateOptions.None )
{
Contract.Requires( !String.IsNullOrWhiteSpace( pattern ) );
Contract.Ensures( Contract.Result<IEnumerable<string>>() != null );
return QuickIODirectory.EnumerateFilePaths( FullNameUnc, pattern, searchOption, pathFormatReturn, enumerateOptions );
}
示例13: Search
/// <summary>
/// Returns the names of files in a specified directories that match the specified patterns.
/// </summary>
/// <param name="source">The directory to search.</param>
/// <param name="fileExtensions">The comma separated list of file extensions. <example>cs,cshtml</example>. </param>
/// <param name="searchStrings">The list of search strings (ignore case).</param>
/// <param name="searchOption">The search options. Default value is AllDirectories.</param>
/// <returns> The list of files that match the specified criteria. </returns>
public static List<SearchMatch> Search(this DirectoryInfo source, string fileExtensions, IEnumerable<string> searchStrings, SearchOption searchOption = SearchOption.AllDirectories)
{
var fileExtensionsList = fileExtensions.Split(',').Select(fileExtension => string.Format(@"*.{0}", fileExtension)).ToList();
var filesToSearch = from searchPattern in fileExtensionsList
from files in Directory.GetFiles(source.FullName, searchPattern, searchOption)
select files;
var matches = new List<SearchMatch>();
foreach (var file in filesToSearch)
{
var lineNumber = 1;
var lines = File.ReadAllLines(file);
foreach (var line in lines)
{
lineNumber++;
matches.AddRange(from searchString in searchStrings
where line.Contains(searchString, StringComparison.OrdinalIgnoreCase)
select new SearchMatch(file, lineNumber, line.Trim()));
}
}
return matches;
}
示例14: FindDatasets
/// <summary>
/// Finds a list of input files within the folder provided.
/// </summary>
/// <param name="folderPath"></param>
/// <returns></returns>
public static List<InputFile> FindDatasets(string folderPath, List<string> extensions, SearchOption options)
{
var datasetList = new List<InputFile>();
var candidates = new List<string>();
foreach (var extension in extensions)
{
var paths = Directory.GetFiles(folderPath, "*" + extension, options);
candidates.AddRange(paths);
}
foreach (var path in candidates)
{
var type = DatasetInformation.GetInputFileType(path);
if (type == InputFileType.NotRecognized)
{
continue;
}
var file = new InputFile();
file.Path = path;
file.FileType = type;
datasetList.Add(file);
}
return datasetList;
}
示例15: GetFiles
/// <summary>
/// Gets information ( array of <see cref="FileInfo"/> objects ), found by files <paramref name="mask"/> according to provided <see cref="SearchOption"/>.
/// </summary>
/// <param name="directory">Directory to search files in.</param>
/// <param name="mask">Search pattern.</param>
/// <param name="searchHow">Search option.</param>
/// <returns>Array of <see cref="FileInfo"/> objects, containing information about files, that has been found.</returns>
/// <exception cref="DirectoryNotFoundException" />
public static FileInfo[] GetFiles( string directory, string mask, SearchOption searchHow )
{
if ( Directory.Exists(directory) )
return new DirectoryInfo(directory).GetFiles(mask, searchHow);
throw new DirectoryNotFoundException(String.Format("Directory '{0}' can't be found.", directory));
}