LocalExplorer/Program.cs

85 lines
3.0 KiB
C#
Raw Permalink Normal View History

2022-04-21 16:00:27 -04:00
/*
* REFERENCES:
2022-04-21 16:00:27 -04:00
* https://www.joe0.com/2019/01/27/how-create-a-custom-browser-uri-scheme-and-c-protocol-handler-client-that-supports-opening-and-editing-of-remotely-hosted-documents-through-webdav/
* https://stackoverflow.com/questions/44675085/minimize-console-at-app-startup-c-sharp
*
2022-04-21 16:00:27 -04:00
*/
using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
2022-04-21 16:00:27 -04:00
namespace LocalExplorer
{
class Program
{
[DllImport("User32.dll", CallingConvention = CallingConvention.StdCall, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool ShowWindow([In] IntPtr hWnd, [In] int nCmdShow);
2022-04-21 16:00:27 -04:00
static void Main(string[] args)
{
//Start this process hidden
IntPtr handle = Process.GetCurrentProcess().MainWindowHandle;
ShowWindow(handle, 0);
2022-04-21 16:00:27 -04:00
//Check to make sure a path was passed in as an argument to the program.
if (args.Length == 0)
{
Console.WriteLine("usage: LocalExplorer.exe <path to open with Explorer.exe>");
WriteOutputFile("no args passed to program");
Environment.Exit(1);
}
//Collect up the path
//the replace function is here to trim out the arg as it
//comes in from the url protocol handler
string path = args[0].Replace("localexplorer:", "").Replace("%5C", "\\");
//If there is a space in the path as it comes from the URL
//protocol handler, replace it with an actual space.
path = path.Replace("%20", " ");
2022-04-21 16:00:27 -04:00
WriteOutputFile($"arg passed: {path}");
//Check to see if the directory exists.
//if so, pop it open.
if (Directory.Exists(path))
{
ProcessStartInfo startInfo = new ProcessStartInfo
{
Arguments = path,
FileName = "explorer.exe"
};
//put this inside try/catch block just in case
//explorer.exe has a permission issue or something.
try
{
Process.Start(startInfo);
}
catch
{
Console.WriteLine($"Could not proc explorer.exe to open {path}");
WriteOutputFile($"Could not proc explorer.exe to open {path}");
}
2022-04-21 16:00:27 -04:00
}
else
{
WriteOutputFile($"Directory does not exist: {path}");
Console.WriteLine("Directory does not exist.");
}
} //end main
private static void WriteOutputFile(string debugLine)
{
// comment this out to debug the program
return;
2022-04-21 16:00:27 -04:00
StreamWriter outFile = new StreamWriter(@"C:\LocalExplorer\OUTPUT.txt", true);
2022-04-21 16:00:27 -04:00
outFile.WriteLine(debugLine);
outFile.Close();
}
}
}