Update Program.cs

Update References
Start program hidden
Wrap Process.Start in try/catch block just in case
This commit is contained in:
kougyoku 2022-04-21 16:41:19 -07:00
parent 2a872f6f7b
commit d6cbb58b78
1 changed files with 25 additions and 4 deletions

View File

@ -1,17 +1,28 @@
/* /*
* REFERENCE: * REFERENCES:
* 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://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
*
*/ */
using System; using System;
using System.Diagnostics; using System.Diagnostics;
using System.IO; using System.IO;
using System.Runtime.InteropServices;
namespace LocalExplorer namespace LocalExplorer
{ {
class Program 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);
static void Main(string[] args) static void Main(string[] args)
{ {
//Start this process hidden
IntPtr handle = Process.GetCurrentProcess().MainWindowHandle;
ShowWindow(handle, 0);
//Check to make sure a path was passed in as an argument to the program. //Check to make sure a path was passed in as an argument to the program.
if (args.Length == 0) if (args.Length == 0)
{ {
@ -37,8 +48,18 @@ namespace LocalExplorer
FileName = "explorer.exe" FileName = "explorer.exe"
}; };
//put this inside try/catch block just in case
//explorer.exe has a permission issue or something.
try
{
Process.Start(startInfo); Process.Start(startInfo);
} }
catch
{
Console.WriteLine($"Could not proc explorer.exe to open {path}");
WriteOutputFile($"Could not proc explorer.exe to open {path}");
}
}
else else
{ {
WriteOutputFile($"Directory does not exist: {path}"); WriteOutputFile($"Directory does not exist: {path}");
@ -48,8 +69,8 @@ namespace LocalExplorer
private static void WriteOutputFile(string debugLine) private static void WriteOutputFile(string debugLine)
{ {
return; // comment this out to debug the program
// remove the above return for debugging //return;
StreamWriter outFile = new StreamWriter(@"C:\LocalExplorer\OUTPUT.txt", true); StreamWriter outFile = new StreamWriter(@"C:\LocalExplorer\OUTPUT.txt", true);
outFile.WriteLine(debugLine); outFile.WriteLine(debugLine);