March 11, 2021

C# Windows File Explorer Openers

This class will Open Windows File Explorer and get it to open a directory and optionally select a specified file in the directory

/// <summary>
/// Windows Explorer Helperes
/// </summary>
public class FileExplorerOpener
{
/// <summary>
/// Open Windows File Explorer, in it open a directory and select
/// a specific file
/// </summary>
/// <param name="filePath"></param>
/// <returns></returns>
public bool OpenDirectoryAndSelectFile(string filePath)
{
var fi = new FileInfo(filePath);
var outcome = fi.Exists;
if (outcome)
{
var argument = "/select, \"" + fi.FullName + "\"";
Process.Start("explorer.exe", argument);
}
return outcome;
}

/// <summary>
/// Open Windows Explorer, in it open the directory or if
/// a file path is passed in, the directory in which the
/// file is located
/// </summary>
/// <param name="inputPath">Directory or file path</param>
public void OpenFileDirectory(string inputPath)
{
string path = System.IO.Path.GetDirectoryName(inputPath);
if (System.IO.Directory.Exists(path))
{
Process.Start(path);
}
}
}

No comments: