Skip to content

Instantly share code, notes, and snippets.

@asquigglytwist
Last active April 4, 2018 09:12
Show Gist options
  • Save asquigglytwist/02f9c9acec92b4bd03e51c764cdf2952 to your computer and use it in GitHub Desktop.
Save asquigglytwist/02f9c9acec92b4bd03e51c764cdf2952 to your computer and use it in GitHub Desktop.
A few helper functions for assisting with operations on a Directory.
public static List<string> GetFileNamesWithoutExtensionAsList(string dir, string pattern)
{
if (Directory.Exists(dir))
{
return new DirectoryInfo(dir).GetFiles(pattern, SearchOption.TopDirectoryOnly)
.Select(file => Path.GetFileNameWithoutExtension(file.Name)).ToList();
}
return new List<string>();
}
public static IEnumerable<string> GetFileNamesWithoutExtensionAsIEnumerable(string dir, string pattern)
{
if (Directory.Exists(dir))
{
return new DirectoryInfo(dir).GetFiles(pattern, SearchOption.TopDirectoryOnly)
.Select(file => Path.GetFileNameWithoutExtension(file.Name));
}
return new List<string>();
}
public static void CreateDirectory(string dirPath)
{
if (!Directory.Exists(dirPath))
{
Directory.CreateDirectory(dirPath);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment