Skip to content

Instantly share code, notes, and snippets.

View JeremyKuhne's full-sized avatar
🏠
Working from home

Jeremy Kuhne JeremyKuhne

🏠
Working from home
  • Microsoft
  • Redmond, WA
View GitHub Profile
@JeremyKuhne
JeremyKuhne / CustomT4.targets
Created June 7, 2016 00:28
Custom targets to allow VS T4 templates to pick up build properties
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<!-- Include this target at the end of your project file to allow properties to be fully initialized -->
<!-- Set up common properties for T4 text templating -->
<ItemGroup>
<!-- Release, debug, etc. -->
<T4ParameterValues Include="Configuration">
<Value>$(Configuration)</Value>
</T4ParameterValues>
@JeremyKuhne
JeremyKuhne / NewPath.cs
Last active March 8, 2018 00:49
New Path APIs in 2.1
namespace System.IO
{
public static class Path
{
public static ReadOnlySpan<char> GetExtension(ReadOnlySpan<char> path);
public static ReadOnlySpan<char> GetFileName(ReadOnlySpan<char> path);
public static ReadOnlySpan<char> GetFileNameWithoutExtension(ReadOnlySpan<char> path);
public static bool HasExtension(ReadOnlySpan<char> path);
public static bool IsPathFullyQualified(ReadOnlySpan<char> path);
public static bool IsPathRooted(ReadOnlySpan<char> path);
@JeremyKuhne
JeremyKuhne / EnumerationOptions.cs
Created March 8, 2018 01:06
New enumeration options in .NET Core 2.1
namespace System.IO
{
public class EnumerationOptions
{
/// <summary>
/// Default constructor. Constructs the options class with recommended default options.
/// </summary>
public EnumerationOptions()
{
IgnoreInaccessible = true;
@JeremyKuhne
JeremyKuhne / FileSystemEnumerable.cs
Created March 8, 2018 05:42
FileSystemEnumerable
namespace System.IO.Enumeration
{
public class FileSystemEnumerable<TResult> : Collections.Generic.IEnumerable<TResult>
{
public FileSystemEnumerable(string directory, FindTransform transform, EnumerationOptions options = null);
// True to recurse into the given directory
public FindPredicate ShouldRecursePredicate { get; set; }
// True to include the given item in the results (through the given transform)
@JeremyKuhne
JeremyKuhne / FileSystemEntry.cs
Created March 8, 2018 05:54
FileSystemEntry
namespace System.IO.Enumeration
{
public ref struct FileSystemEntry
{
// The directory this entry (file/directory) lives in
public ReadOnlySpan<char> Directory { get; }
// The fully qualified root directory of the enumeration
public ReadOnlySpan<char> RootDirectory { get; }
@JeremyKuhne
JeremyKuhne / NameEnumerable.cs
Last active March 8, 2018 06:27
File system name enumerable example
IEnumerable<string> fileAndDirectoryNames =
new FileSystemEnumerable<string>(@"C:\", (ref FileSystemEntry entry) => entry.FileName.ToString());
@JeremyKuhne
JeremyKuhne / FilenameEnumerable.cs
Created March 8, 2018 06:35
File name enumerable example
IEnumerable<string> fileNames =
new FileSystemEnumerable<string>(
@"C:\test",
(ref FileSystemEntry entry) => entry.FileName.ToString())
{
ShouldIncludePredicate = (ref FileSystemEntry entry) => !entry.IsDirectory
};
@JeremyKuhne
JeremyKuhne / GetFilesWithExtensions.cs
Last active March 10, 2018 04:15
Sample to get files with given extensions
public static IEnumerable<FileInfo> GetFilesWithExtensions(string directory,
bool recursive, params string[] extensions)
{
return new FileSystemEnumerable<FileInfo>(
directory,
(ref FileSystemEntry entry) => (FileInfo)entry.ToFileSystemInfo(),
new EnumerationOptions() { RecurseSubdirectories = recursive })
{
ShouldIncludePredicate = (ref FileSystemEntry entry) =>
{
@JeremyKuhne
JeremyKuhne / CountFiles.cs
Created March 8, 2018 23:58
Counting files example
public static int CountFiles(string directory, bool recursive)
{
return (new FileSystemEnumerable<int>(
directory,
(ref FileSystemEntry entry) => 1,
new EnumerationOptions() { RecurseSubdirectories = recursive })
{
ShouldIncludePredicate = (ref FileSystemEntry entry) => !entry.IsDirectory
}).Count();
}
@JeremyKuhne
JeremyKuhne / CountFileBytes.cs
Created March 9, 2018 00:05
Counting file bytes example
public static long CountFileBytes(string directory, bool recursive)
{
return (new FileSystemEnumerable<long>(
directory,
(ref FileSystemEntry entry) => entry.Length,
new EnumerationOptions() { RecurseSubdirectories = recursive })
{
ShouldIncludePredicate = (ref FileSystemEntry entry) => !entry.IsDirectory
}).Sum();
}