本文整理汇总了C#中String.SelectMany方法的典型用法代码示例。如果您正苦于以下问题:C# String.SelectMany方法的具体用法?C# String.SelectMany怎么用?C# String.SelectMany使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类String
的用法示例。
在下文中一共展示了String.SelectMany方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetDirectories
/// <summary>
/// Returns an enumerable collection of directory names that match a search pattern in a specified @this.
/// </summary>
/// <param name="this">The directory to search.</param>
/// <param name="searchPatterns">The search string to match against the names of directories in.</param>
/// <returns>
/// An enumerable collection of the full names (including paths) for the directories in the directory specified by
/// <paramref
/// name="this" />
/// and that match the specified search pattern.
/// </returns>
/// ###
/// <param name="searchPattern">
/// The search string to match against the names of directories in
/// <paramref name="this" />.
/// </param>
/// ###
/// <exception cref="T:System.ArgumentException">
/// <paramref name="this " />is a zero-length string, contains only
/// white space, or contains invalid characters as defined by
/// <see
/// cref="M:System.IO.Path.GetInvalidPathChars" />
/// .- or -<paramref name="searchPattern" /> does not contain a valid pattern.
/// </exception>
/// ###
/// <exception cref="T:System.ArgumentNullException">
/// <paramref name="this" /> is null.-or-
/// <paramref name="searchPattern" /> is null.
/// </exception>
/// ###
/// <exception cref="T:System.IO.DirectoryNotFoundException">
/// <paramref name="this" /> is invalid, such as
/// referring to an unmapped drive.
/// </exception>
/// ###
/// <exception cref="T:System.IO.IOException">
/// <paramref name="this" /> is a file name.
/// </exception>
/// ###
/// <exception cref="T:System.IO.PathTooLongException">
/// The specified @this, file name, or combined exceed the
/// system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters
/// and file names must be less than 260 characters.
/// </exception>
/// ###
/// <exception cref="T:System.Security.SecurityException">The caller does not have the required permission.</exception>
/// ###
/// <exception cref="T:System.UnauthorizedAccessException">The caller does not have the required permission.</exception>
public static DirectoryInfo[] GetDirectories(this DirectoryInfo @this, String[] searchPatterns)
{
return searchPatterns.SelectMany(x => @this.GetDirectories(x)).Distinct().ToArray();
}
示例2: EnumerateFileSystemEntries
//.........这里部分代码省略.........
/// {
/// // Type
/// var root = new DirectoryInfo(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "System_IO_DirectoryInfo_EnumerateFileSystemEntries"));
/// Directory.CreateDirectory(root.FullName);
///
/// var file1 = new FileInfo(Path.Combine(root.FullName, "test1.txt"));
/// file1.Create();
/// root.CreateSubdirectory("test2");
///
/// // Exemples
/// List<string> result = root.EnumerateFileSystemEntries().ToList();
///
/// // Unit Test
/// Assert.AreEqual(2, result.Count);
/// }
/// }
/// }
/// </code>
/// </example>
/// ###
/// <param name="searchPattern">
/// The search string to match against the names of directories in
/// <paramref name="this" />.
/// </param>
/// ###
/// <exception cref="T:System.ArgumentException">
/// <paramref name="this " />is a zero-length string, contains only
/// white space, or contains invalid characters as defined by
/// <see
/// cref="M:System.IO.Path.GetInvalidPathChars" />
/// .- or -<paramref name="searchPattern" /> does not contain a valid pattern.
/// </exception>
/// ###
/// <exception cref="T:System.ArgumentNullException">
/// <paramref name="this" /> is null.-or-
/// <paramref name="searchPattern" /> is null.
/// </exception>
/// ###
/// <exception cref="T:System.IO.DirectoryNotFoundException">
/// <paramref name="this" /> is invalid, such as
/// referring to an unmapped drive.
/// </exception>
/// ###
/// <exception cref="T:System.IO.IOException">
/// <paramref name="this" /> is a file name.
/// </exception>
/// ###
/// <exception cref="T:System.IO.PathTooLongException">
/// The specified @this, file name, or combined exceed the
/// system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters
/// and file names must be less than 260 characters.
/// </exception>
/// ###
/// <exception cref="T:System.Security.SecurityException">The caller does not have the required permission.</exception>
/// ###
/// <exception cref="T:System.UnauthorizedAccessException">The caller does not have the required permission.</exception>
/// <example>
/// <code>
/// using System;
/// using System.Collections.Generic;
/// using System.IO;
/// using System.Linq;
/// using Microsoft.VisualStudio.TestTools.UnitTesting;
/// using Z.ExtensionMethods;
///
/// namespace ExtensionMethods.Examples
/// {
/// [TestClass]
/// public class System_IO_DirectoryInfo_EnumerateFileSystemEntries
/// {
/// [TestMethod]
/// public void EnumerateFileSystemEntries()
/// {
/// // Type
/// var root = new DirectoryInfo(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "System_IO_DirectoryInfo_EnumerateFileSystemEntries"));
/// Directory.CreateDirectory(root.FullName);
///
/// var file1 = new FileInfo(Path.Combine(root.FullName, "test1.txt"));
/// file1.Create();
/// root.CreateSubdirectory("test2");
///
/// // Exemples
/// List<string> result = root.EnumerateFileSystemEntries().ToList();
///
/// // Unit Test
/// Assert.AreEqual(2, result.Count);
/// }
/// }
/// }
/// </code>
/// </example>
public static IEnumerable<string> EnumerateFileSystemEntries(this DirectoryInfo @this, String[] searchPatterns)
{
#if (NET35 || NET3 || NET2)
return searchPatterns.SelectMany(x => Directory.GetFileSystemEntries(@this.FullName, x)).Distinct();
#else
return searchPatterns.SelectMany(x => Directory.EnumerateFileSystemEntries(@this.FullName, x)).Distinct();
#endif
}
示例3: EnumerateFiles
//.........这里部分代码省略.........
/// public class System_IO_DirectoryInfo_EnumerateFiles
/// {
/// [TestMethod]
/// public void EnumerateFiles()
/// {
/// // Type
/// var root = new DirectoryInfo(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "System_IO_DirectoryInfo_EnumerateFiles"));
/// Directory.CreateDirectory(root.FullName);
///
/// var file1 = new FileInfo(Path.Combine(root.FullName, "test1.txt"));
/// var file2 = new FileInfo(Path.Combine(root.FullName, "test2.txt"));
/// file1.Create();
/// file2.Create();
///
/// // Exemples
/// List<FileInfo> result = root.EnumerateFiles().ToList();
///
/// // Unit Test
/// Assert.AreEqual(2, result.Count);
/// }
/// }
/// }
/// </code>
/// </example>
/// ###
/// <exception cref="T:System.ArgumentException">
/// <paramref name="this " />is a zero-length string, contains only
/// white space, or contains invalid characters as defined by
/// <see
/// cref="M:System.IO.Path.GetInvalidPathChars" />
/// .- or -<paramref name="searchPattern" /> does not contain a valid pattern.
/// </exception>
/// ###
/// <exception cref="T:System.ArgumentNullException">
/// <paramref name="this" /> is null.-or-
/// <paramref name="searchPattern" /> is null.
/// </exception>
/// ###
/// <exception cref="T:System.ArgumentOutOfRangeException">
/// <paramref name="searchOption" /> is not a valid
/// <see cref="T:System.IO.SearchOption" /> value.
/// </exception>
/// ###
/// <exception cref="T:System.IO.DirectoryNotFoundException">
/// <paramref name="this" /> is invalid, such as
/// referring to an unmapped drive.
/// </exception>
/// ###
/// <exception cref="T:System.IO.IOException">
/// <paramref name="this" /> is a file name.
/// </exception>
/// ###
/// <exception cref="T:System.IO.PathTooLongException">
/// The specified @this, file name, or combined exceed the
/// system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters
/// and file names must be less than 260 characters.
/// </exception>
/// ###
/// <exception cref="T:System.Security.SecurityException">The caller does not have the required permission.</exception>
/// ###
/// <exception cref="T:System.UnauthorizedAccessException">The caller does not have the required permission.</exception>
/// <example>
/// <code>
/// using System;
/// using System.Collections.Generic;
/// using System.IO;
/// using System.Linq;
/// using Microsoft.VisualStudio.TestTools.UnitTesting;
///
/// namespace ExtensionMethods.Examples
/// {
/// [TestClass]
/// public class System_IO_DirectoryInfo_EnumerateFiles
/// {
/// [TestMethod]
/// public void EnumerateFiles()
/// {
/// // Type
/// var root = new DirectoryInfo(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "System_IO_DirectoryInfo_EnumerateFiles"));
/// Directory.CreateDirectory(root.FullName);
///
/// var file1 = new FileInfo(Path.Combine(root.FullName, "test1.txt"));
/// var file2 = new FileInfo(Path.Combine(root.FullName, "test2.txt"));
/// file1.Create();
/// file2.Create();
///
/// // Exemples
/// List<FileInfo> result = root.EnumerateFiles().ToList();
///
/// // Unit Test
/// Assert.AreEqual(2, result.Count);
/// }
/// }
/// }
/// </code>
/// </example>
public static IEnumerable<FileInfo> EnumerateFiles(this DirectoryInfo @this, String[] searchPatterns, SearchOption searchOption)
{
return searchPatterns.SelectMany(x => @this.GetFiles(x, searchOption)).Distinct();
}
示例4: GetFilesWhere
//.........这里部分代码省略.........
/// public void GetFilesWhere()
/// {
/// // Type
/// var root = new DirectoryInfo(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "System_IO_DirectoryInfo_GetFilesWhere"));
/// Directory.CreateDirectory(root.FullName);
///
/// var file1 = new FileInfo(Path.Combine(root.FullName, "test.txt"));
/// var file2 = new FileInfo(Path.Combine(root.FullName, "test.cs"));
/// var file3 = new FileInfo(Path.Combine(root.FullName, "test.asp"));
/// file1.Create();
/// file2.Create();
/// file3.Create();
///
/// // Exemples
/// FileInfo[] result = root.GetFilesWhere(x => x.Extension == ".txt" || x.Extension == ".cs");
///
/// // Unit Test
/// Assert.AreEqual(2, result.Length);
/// }
/// }
/// }
/// </code>
/// </example>
/// <example>
/// <code>
/// using System;
/// using System.IO;
/// using Microsoft.VisualStudio.TestTools.UnitTesting;
/// using Z.ExtensionMethods;
///
/// namespace ExtensionMethods.Examples
/// {
/// [TestClass]
/// public class System_IO_DirectoryInfo_GetFilesWhere
/// {
/// [TestMethod]
/// public void GetFilesWhere()
/// {
/// // Type
/// var root = new DirectoryInfo(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "System_IO_DirectoryInfo_GetFilesWhere"));
/// Directory.CreateDirectory(root.FullName);
///
/// var file1 = new FileInfo(Path.Combine(root.FullName, "test.txt"));
/// var file2 = new FileInfo(Path.Combine(root.FullName, "test.cs"));
/// var file3 = new FileInfo(Path.Combine(root.FullName, "test.asp"));
/// file1.Create();
/// file2.Create();
/// file3.Create();
///
/// // Exemples
/// FileInfo[] result = root.GetFilesWhere(x => x.Extension == ".txt" || x.Extension == ".cs");
///
/// // Unit Test
/// Assert.AreEqual(2, result.Length);
/// }
/// }
/// }
/// </code>
/// </example>
/// ###
/// <exception cref="T:System.ArgumentException">
/// <paramref name="this " />is a zero-length string, contains only
/// white space, or contains invalid characters as defined by
/// <see
/// cref="M:System.IO.Path.GetInvalidPathChars" />
/// .- or -<paramref name="searchPattern" /> does not contain a valid pattern.
/// </exception>
/// ###
/// <exception cref="T:System.ArgumentNullException">
/// <paramref name="this" /> is null.-or-
/// <paramref name="searchPattern" /> is null.
/// </exception>
/// ###
/// <exception cref="T:System.ArgumentOutOfRangeException">
/// <paramref name="searchOption" /> is not a valid
/// <see cref="T:System.IO.SearchOption" /> value.
/// </exception>
/// ###
/// <exception cref="T:System.IO.DirectoryNotFoundException">
/// <paramref name="this" /> is invalid, such as
/// referring to an unmapped drive.
/// </exception>
/// ###
/// <exception cref="T:System.IO.IOException">
/// <paramref name="this" /> is a file name.
/// </exception>
/// ###
/// <exception cref="T:System.IO.PathTooLongException">
/// The specified @this, file name, or combined exceed the
/// system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters
/// and file names must be less than 260 characters.
/// </exception>
/// ###
/// <exception cref="T:System.Security.SecurityException">The caller does not have the required permission.</exception>
/// ###
/// <exception cref="T:System.UnauthorizedAccessException">The caller does not have the required permission.</exception>
public static FileInfo[] GetFilesWhere(this DirectoryInfo @this, String[] searchPatterns, SearchOption searchOption, Func<FileInfo, bool> predicate)
{
return searchPatterns.SelectMany(x => @this.GetFiles(x, searchOption)).Distinct().Where(x => predicate(x)).ToArray();
}
示例5: EnumerateDirectories
//.........这里部分代码省略.........
/// using System.Linq;
/// using Microsoft.VisualStudio.TestTools.UnitTesting;
///
/// namespace ExtensionMethods.Examples
/// {
/// [TestClass]
/// public class System_IO_DirectoryInfo_EnumerateDirectories
/// {
/// [TestMethod]
/// public void EnumerateDirectories()
/// {
/// // Type
/// var root = new DirectoryInfo(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "System_IO_DirectoryInfo_EnumerateDirectories"));
/// Directory.CreateDirectory(root.FullName);
/// root.CreateSubdirectory("Directory1");
/// root.CreateSubdirectory("Directory2");
///
/// // Exemples
/// List<DirectoryInfo> result = root.EnumerateDirectories().ToList();
///
/// // Unit Test
/// Assert.AreEqual(2, result.Count);
/// }
/// }
/// }
/// </code>
/// </example>
/// ###
/// <param name="searchPattern">
/// The search string to match against the names of directories in
/// <paramref name="this" />.
/// </param>
/// ###
/// <exception cref="T:System.ArgumentException">
/// <paramref name="this " />is a zero-length string, contains only
/// white space, or contains invalid characters as defined by
/// <see
/// cref="M:System.IO.Path.GetInvalidPathChars" />
/// .- or -<paramref name="searchPattern" /> does not contain a valid pattern.
/// </exception>
/// ###
/// <exception cref="T:System.ArgumentNullException">
/// <paramref name="this" /> is null.-or-
/// <paramref name="searchPattern" /> is null.
/// </exception>
/// ###
/// <exception cref="T:System.IO.DirectoryNotFoundException">
/// <paramref name="this" /> is invalid, such as
/// referring to an unmapped drive.
/// </exception>
/// ###
/// <exception cref="T:System.IO.IOException">
/// <paramref name="this" /> is a file name.
/// </exception>
/// ###
/// <exception cref="T:System.IO.PathTooLongException">
/// The specified @this, file name, or combined exceed the
/// system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters
/// and file names must be less than 260 characters.
/// </exception>
/// ###
/// <exception cref="T:System.Security.SecurityException">The caller does not have the required permission.</exception>
/// ###
/// <exception cref="T:System.UnauthorizedAccessException">The caller does not have the required permission.</exception>
/// <example>
/// <code>
/// using System;
/// using System.Collections.Generic;
/// using System.IO;
/// using System.Linq;
/// using Microsoft.VisualStudio.TestTools.UnitTesting;
///
/// namespace ExtensionMethods.Examples
/// {
/// [TestClass]
/// public class System_IO_DirectoryInfo_EnumerateDirectories
/// {
/// [TestMethod]
/// public void EnumerateDirectories()
/// {
/// // Type
/// var root = new DirectoryInfo(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "System_IO_DirectoryInfo_EnumerateDirectories"));
/// Directory.CreateDirectory(root.FullName);
/// root.CreateSubdirectory("Directory1");
/// root.CreateSubdirectory("Directory2");
///
/// // Exemples
/// List<DirectoryInfo> result = root.EnumerateDirectories().ToList();
///
/// // Unit Test
/// Assert.AreEqual(2, result.Count);
/// }
/// }
/// }
/// </code>
/// </example>
public static IEnumerable<DirectoryInfo> EnumerateDirectories(this DirectoryInfo @this, String[] searchPatterns)
{
return searchPatterns.SelectMany(x => @this.GetDirectories(x)).Distinct();
}
示例6: GetFileSystemEntriesWhere
//.........这里部分代码省略.........
/// namespace ExtensionMethods.Examples
/// {
/// [TestClass]
/// public class System_IO_DirectoryInfo_GetFileSystemEntriesWhere
/// {
/// [TestMethod]
/// public void GetFileSystemEntriesWhere()
/// {
/// // Type
/// var root = new DirectoryInfo(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "System_IO_DirectoryInfo_GetFileSystemEntriesWhere"));
/// Directory.CreateDirectory(root.FullName);
/// root.CreateSubdirectory("DirFizz123");
/// root.CreateSubdirectory("DirBuzz123");
/// var file1 = new FileInfo(Path.Combine(root.FullName, "test1.txt"));
/// file1.Create();
///
/// // Exemples
/// string[] result = root.GetFileSystemEntriesWhere(x => x.Contains("DirFizz") || x.EndsWith(".txt"));
///
/// // Unit Test
/// Assert.AreEqual(2, result.Length);
/// }
/// }
/// }
/// </code>
/// </example>
/// <example>
/// <code>
/// using System;
/// using System.IO;
/// using Microsoft.VisualStudio.TestTools.UnitTesting;
/// using Z.ExtensionMethods;
///
/// namespace ExtensionMethods.Examples
/// {
/// [TestClass]
/// public class System_IO_DirectoryInfo_GetFileSystemEntriesWhere
/// {
/// [TestMethod]
/// public void GetFileSystemEntriesWhere()
/// {
/// // Type
/// var root = new DirectoryInfo(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "System_IO_DirectoryInfo_GetFileSystemEntriesWhere"));
/// Directory.CreateDirectory(root.FullName);
/// root.CreateSubdirectory("DirFizz123");
/// root.CreateSubdirectory("DirBuzz123");
/// var file1 = new FileInfo(Path.Combine(root.FullName, "test1.txt"));
/// file1.Create();
///
/// // Exemples
/// string[] result = root.GetFileSystemEntriesWhere(x => x.Contains("DirFizz") || x.EndsWith(".txt"));
///
/// // Unit Test
/// Assert.AreEqual(2, result.Length);
/// }
/// }
/// }
/// </code>
/// </example>
/// ###
/// <exception cref="T:System.ArgumentException">
/// <paramref name="this " />is a zero-length string, contains only
/// white space, or contains invalid characters as defined by
/// <see
/// cref="M:System.IO.Path.GetInvalidPathChars" />
/// .- or -<paramref name="searchPattern" /> does not contain a valid pattern.
/// </exception>
/// ###
/// <exception cref="T:System.ArgumentNullException">
/// <paramref name="this" /> is null.-or-
/// <paramref name="searchPattern" /> is null.
/// </exception>
/// ###
/// <exception cref="T:System.ArgumentOutOfRangeException">
/// <paramref name="searchOption" /> is not a valid
/// <see cref="T:System.IO.SearchOption" /> value.
/// </exception>
/// ###
/// <exception cref="T:System.IO.DirectoryNotFoundException">
/// <paramref name="this" /> is invalid, such as
/// referring to an unmapped drive.
/// </exception>
/// ###
/// <exception cref="T:System.IO.IOException">
/// <paramref name="this" /> is a file name.
/// </exception>
/// ###
/// <exception cref="T:System.IO.PathTooLongException">
/// The specified @this, file name, or combined exceed the
/// system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters
/// and file names must be less than 260 characters.
/// </exception>
/// ###
/// <exception cref="T:System.Security.SecurityException">The caller does not have the required permission.</exception>
/// ###
/// <exception cref="T:System.UnauthorizedAccessException">The caller does not have the required permission.</exception>
public static string[] GetFileSystemEntriesWhere(this DirectoryInfo @this, String[] searchPatterns, SearchOption searchOption, Func<string, bool> predicate)
{
return searchPatterns.SelectMany(x => Directory.EnumerateFileSystemEntries(@this.FullName, x, searchOption)).Distinct().Where(x => predicate(x)).ToArray();
}
示例7: GetDirectoriesWhere
//.........这里部分代码省略.........
///
///
/// namespace ExtensionMethods.Examples
/// {
/// [TestClass]
/// public class System_IO_DirectoryInfo_GetDirectoriesWhere
/// {
/// [TestMethod]
/// public void GetDirectoriesWhere()
/// {
/// // Type
/// var root = new DirectoryInfo(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "System_IO_DirectoryInfo_GetDirectories"));
/// Directory.CreateDirectory(root.FullName);
/// root.CreateSubdirectory("DirFizz123");
/// root.CreateSubdirectory("DirBuzz123");
/// root.CreateSubdirectory("DirNotFound123");
///
/// // Exemples
/// DirectoryInfo[] result = root.GetDirectoriesWhere(x => x.Name.StartsWith("DirFizz") || x.Name.StartsWith("DirBuzz"));
///
/// // Unit Test
/// Assert.AreEqual(2, result.Length);
/// }
/// }
/// }
/// </code>
/// </example>
/// <example>
/// <code>
/// using System;
/// using System.IO;
/// using Microsoft.VisualStudio.TestTools.UnitTesting;
/// using Z.ExtensionMethods;
///
/// namespace ExtensionMethods.Examples
/// {
/// [TestClass]
/// public class System_IO_DirectoryInfo_GetDirectoriesWhere
/// {
/// [TestMethod]
/// public void GetDirectoriesWhere()
/// {
/// // Type
/// var root = new DirectoryInfo(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "System_IO_DirectoryInfo_GetDirectories"));
/// Directory.CreateDirectory(root.FullName);
/// root.CreateSubdirectory("DirFizz123");
/// root.CreateSubdirectory("DirBuzz123");
/// root.CreateSubdirectory("DirNotFound123");
///
/// // Exemples
/// DirectoryInfo[] result = root.GetDirectoriesWhere(x => x.Name.StartsWith("DirFizz") || x.Name.StartsWith("DirBuzz"));
///
/// // Unit Test
/// Assert.AreEqual(2, result.Length);
/// }
/// }
/// }
/// </code>
/// </example>
/// ###
/// <param name="searchPattern">
/// The search string to match against the names of directories in
/// <paramref name="this" />.
/// </param>
/// ###
/// <exception cref="T:System.ArgumentException">
/// <paramref name="this " />is a zero-length string, contains only
/// white space, or contains invalid characters as defined by
/// <see
/// cref="M:System.IO.Path.GetInvalidPathChars" />
/// .- or -<paramref name="searchPattern" /> does not contain a valid pattern.
/// </exception>
/// ###
/// <exception cref="T:System.ArgumentNullException">
/// <paramref name="this" /> is null.-or-
/// <paramref name="searchPattern" /> is null.
/// </exception>
/// ###
/// <exception cref="T:System.IO.DirectoryNotFoundException">
/// <paramref name="this" /> is invalid, such as
/// referring to an unmapped drive.
/// </exception>
/// ###
/// <exception cref="T:System.IO.IOException">
/// <paramref name="this" /> is a file name.
/// </exception>
/// ###
/// <exception cref="T:System.IO.PathTooLongException">
/// The specified @this, file name, or combined exceed the
/// system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters
/// and file names must be less than 260 characters.
/// </exception>
/// ###
/// <exception cref="T:System.Security.SecurityException">The caller does not have the required permission.</exception>
/// ###
/// <exception cref="T:System.UnauthorizedAccessException">The caller does not have the required permission.</exception>
public static DirectoryInfo[] GetDirectoriesWhere(this DirectoryInfo @this, String[] searchPatterns, Func<DirectoryInfo, bool> predicate)
{
return searchPatterns.SelectMany(x => @this.GetDirectories(x)).Distinct().Where(x => predicate(x)).ToArray();
}
示例8: GetFileSystemEntries
//.........这里部分代码省略.........
/// namespace ExtensionMethods.Examples
/// {
/// [TestClass]
/// public class System_IO_DirectoryInfo_GetFileSystemEntries
/// {
/// [TestMethod]
/// public void GetFileSystemEntries()
/// {
/// // Type
/// var root = new DirectoryInfo(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "System_IO_DirectoryInfo_GetFileSystemEntries"));
/// Directory.CreateDirectory(root.FullName);
/// root.CreateSubdirectory("DirFizz123");
/// root.CreateSubdirectory("DirBuzz123");
/// var file1 = new FileInfo(Path.Combine(root.FullName, "test1.txt"));
/// file1.Create();
///
/// // Exemples
/// string[] result = root.GetFileSystemEntries(new[] {"DirFizz*", "*.txt"});
///
/// // Unit Test
/// Assert.AreEqual(2, result.Length);
/// }
/// }
/// }
/// </code>
/// </example>
/// <example>
/// <code>
/// using System;
/// using System.IO;
/// using Microsoft.VisualStudio.TestTools.UnitTesting;
/// using Z.ExtensionMethods;
///
/// namespace ExtensionMethods.Examples
/// {
/// [TestClass]
/// public class System_IO_DirectoryInfo_GetFileSystemEntries
/// {
/// [TestMethod]
/// public void GetFileSystemEntries()
/// {
/// // Type
/// var root = new DirectoryInfo(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "System_IO_DirectoryInfo_GetFileSystemEntries"));
/// Directory.CreateDirectory(root.FullName);
/// root.CreateSubdirectory("DirFizz123");
/// root.CreateSubdirectory("DirBuzz123");
/// var file1 = new FileInfo(Path.Combine(root.FullName, "test1.txt"));
/// file1.Create();
///
/// // Exemples
/// string[] result = root.GetFileSystemEntries(new[] {"DirFizz*", "*.txt"});
///
/// // Unit Test
/// Assert.AreEqual(2, result.Length);
/// }
/// }
/// }
/// </code>
/// </example>
/// ###
/// <param name="searchPattern">
/// The search string to match against the names of directories in
/// <paramref name="this" />.
/// </param>
/// ###
/// <exception cref="T:System.ArgumentException">
/// <paramref name="this " />is a zero-length string, contains only
/// white space, or contains invalid characters as defined by
/// <see
/// cref="M:System.IO.Path.GetInvalidPathChars" />
/// .- or -<paramref name="searchPattern" /> does not contain a valid pattern.
/// </exception>
/// ###
/// <exception cref="T:System.ArgumentNullException">
/// <paramref name="this" /> is null.-or-
/// <paramref name="searchPattern" /> is null.
/// </exception>
/// ###
/// <exception cref="T:System.IO.DirectoryNotFoundException">
/// <paramref name="this" /> is invalid, such as
/// referring to an unmapped drive.
/// </exception>
/// ###
/// <exception cref="T:System.IO.IOException">
/// <paramref name="this" /> is a file name.
/// </exception>
/// ###
/// <exception cref="T:System.IO.PathTooLongException">
/// The specified @this, file name, or combined exceed the
/// system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters
/// and file names must be less than 260 characters.
/// </exception>
/// ###
/// <exception cref="T:System.Security.SecurityException">The caller does not have the required permission.</exception>
/// ###
/// <exception cref="T:System.UnauthorizedAccessException">The caller does not have the required permission.</exception>
public static string[] GetFileSystemEntries(this DirectoryInfo @this, String[] searchPatterns)
{
return searchPatterns.SelectMany(x => Directory.EnumerateFileSystemEntries(@this.FullName, x)).Distinct().ToArray();
}
示例9: GetFiles
/// <summary>
/// Returns an enumerable collection of file names that match a search pattern in a specified @this, and
/// optionally searches subdirectories.
/// </summary>
/// <param name="this">The directory to search.</param>
/// <param name="searchPatterns">
/// The search string to match against the names of directories in
/// <paramref name="this" />.
/// </param>
/// <param name="searchOption">
/// One of the enumeration values that specifies whether the search operation should
/// include only the current directory or should include all subdirectories.The default value is
/// <see
/// cref="F:System.IO.SearchOption.TopDirectoryOnly" />
/// .
/// </param>
/// <returns>
/// An enumerable collection of the full names (including paths) for the files in the directory specified by
/// <paramref
/// name="this" />
/// and that match the specified search pattern and option.
/// </returns>
/// ###
/// <exception cref="T:System.ArgumentException">
/// <paramref name="this " />is a zero-length string, contains only
/// white space, or contains invalid characters as defined by
/// <see
/// cref="M:System.IO.Path.GetInvalidPathChars" />
/// .- or -<paramref name="searchPattern" /> does not contain a valid pattern.
/// </exception>
/// ###
/// <exception cref="T:System.ArgumentNullException">
/// <paramref name="this" /> is null.-or-
/// <paramref name="searchPattern" /> is null.
/// </exception>
/// ###
/// <exception cref="T:System.ArgumentOutOfRangeException">
/// <paramref name="searchOption" /> is not a valid
/// <see cref="T:System.IO.SearchOption" /> value.
/// </exception>
/// ###
/// <exception cref="T:System.IO.DirectoryNotFoundException">
/// <paramref name="this" /> is invalid, such as
/// referring to an unmapped drive.
/// </exception>
/// ###
/// <exception cref="T:System.IO.IOException">
/// <paramref name="this" /> is a file name.
/// </exception>
/// ###
/// <exception cref="T:System.IO.PathTooLongException">
/// The specified @this, file name, or combined exceed the
/// system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters
/// and file names must be less than 260 characters.
/// </exception>
/// ###
/// <exception cref="T:System.Security.SecurityException">The caller does not have the required permission.</exception>
/// ###
/// <exception cref="T:System.UnauthorizedAccessException">The caller does not have the required permission.</exception>
public static FileInfo[] GetFiles(this DirectoryInfo @this, String[] searchPatterns, SearchOption searchOption)
{
return searchPatterns.SelectMany(x => @this.GetFiles(x, searchOption)).Distinct().ToArray();
}