Allows opening of files with ALLOWED_EXTENSIONS from a fileserver with PREFIX eg, contoso.com\ . The PREFIX and ALLOWED_EXTENSIONS are configurable in App.config
This commit is contained in:
kougyoku 2022-05-10 13:45:39 -07:00
parent 16c9c8f9b4
commit 6ae4436119
6 changed files with 214 additions and 26 deletions

View File

@ -1,6 +1,27 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="LocalExplorer.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</sectionGroup>
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
</startup>
<applicationSettings>
<LocalExplorer.Properties.Settings>
<setting name="PREFIX" serializeAs="String">
<value>ssv.wa.lcl\</value>
</setting>
<setting name="ALLOWED_EXTENSIONS" serializeAs="Xml">
<value>
<ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<string>.docx</string>
<string>.pdf</string>
</ArrayOfString>
</value>
</setting>
</LocalExplorer.Properties.Settings>
</applicationSettings>
</configuration>

View File

@ -37,7 +37,9 @@
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Configuration" />
<Reference Include="System.Core" />
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
@ -48,9 +50,18 @@
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Properties\Settings.Designer.cs">
<AutoGen>True</AutoGen>
<DesignTimeSharedInput>True</DesignTimeSharedInput>
<DependentUpon>Settings.settings</DependentUpon>
</Compile>
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
<None Include="Properties\Settings.settings">
<Generator>SettingsSingleFileGenerator</Generator>
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
</None>
</ItemGroup>
<ItemGroup>
<Content Include="LocalExplorer.ico" />

View File

@ -5,9 +5,12 @@
*
*/
using System;
using System.Collections.Specialized;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
namespace LocalExplorer
{
@ -17,8 +20,14 @@ namespace LocalExplorer
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool ShowWindow([In] IntPtr hWnd, [In] int nCmdShow);
[DllImport("shell32.dll", EntryPoint = "FindExecutable")]
private static extern long FindExecutable(string lpFile, string lpDirectory, StringBuilder lpResult);
static void Main(string[] args)
{
bool bDirectoryExists = false;
bool bFileExists = false;
//Start this process hidden
IntPtr handle = Process.GetCurrentProcess().MainWindowHandle;
ShowWindow(handle, 0);
@ -42,35 +51,108 @@ namespace LocalExplorer
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"
};
//Determine if file or directory.
//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}");
}
}
else
//Check to see if the directory or file exists.
if (Directory.Exists(path))
{ bDirectoryExists = true; }
if (!bDirectoryExists && File.Exists(path))
{ bFileExists = true; }
//if the directory exists pop it open.
if (bDirectoryExists)
{
WriteOutputFile($"Directory does not exist: {path}");
Console.WriteLine("Directory does not exist.");
OpenDirectory(path);
}
else if (bFileExists)
{
OpenFile(path);
}
else //The directory or file does not exist.
{
MessageBox.Show($"Directory or File does not exist: {path}");
WriteOutputFile($"Directory or File does not exist: {path}");
Console.WriteLine($"Directory or File does not exist: {path}");
}
} //end main
private static void OpenDirectory(string 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}");
}
}
private static void OpenFile(string pathToFile)
{
string PATH_PREFIX = Properties.Settings.Default.PREFIX;
StringCollection ALLOWED_EXTENSIONS = Properties.Settings.Default.ALLOWED_EXTENSIONS;
//Perform sanity checks.
//Check for ssv.wa.lcl prefix.
if (!pathToFile.Contains(PATH_PREFIX))
{
MessageBox.Show($"Not permitted to open file because path does not contain {PATH_PREFIX} : {pathToFile}");
return;
}
//Check for allowed file extensions. List of allowed extensions is above.
//GetExtension returns .docx as example, strip out to match allowed extensions
string extension = Path.GetExtension(pathToFile);
if (!ALLOWED_EXTENSIONS.Contains(extension))
{
MessageBox.Show($"Not an allowed extension: {pathToFile} ; extension: {extension}");
return;
}
string path = $"{Path.GetTempFileName()}{Path.GetExtension(pathToFile)}";
//Need to create the new temp file.
if (!File.Exists(path))
{
using (StreamWriter sw = File.CreateText(path)) { }
}
if (!HasExecutable(path))
{
MessageBox.Show($"This file does not have an assigned executable: {pathToFile}");
return;
}
string commandToRun = $"\"{FindExecutable(path)}\" \"{pathToFile}\"";
WriteOutputFile($"opening file with command: {commandToRun}");
//LOOKS LIKE YOUR COMMAND LINE JUST GOT EXECUTED
Process cmd = new Process();
cmd.StartInfo.FileName = "cmd.exe";
cmd.StartInfo.RedirectStandardInput = true;
cmd.StartInfo.RedirectStandardOutput = true;
cmd.StartInfo.CreateNoWindow = true;
cmd.StartInfo.UseShellExecute = false;
cmd.Start();
cmd.StandardInput.WriteLine(commandToRun);
cmd.StandardInput.Flush();
cmd.StandardInput.Close();
}
private static void WriteOutputFile(string debugLine)
{
// comment this out to debug the program
@ -80,5 +162,17 @@ namespace LocalExplorer
outFile.WriteLine(debugLine);
outFile.Close();
}
public static bool HasExecutable(string path)
{
var executable = FindExecutable(path);
return !string.IsNullOrEmpty(executable);
}
private static string FindExecutable(string path)
{
var executable = new StringBuilder(1024);
FindExecutable(path, string.Empty, executable);
return executable.ToString();
}
}
}

View File

@ -32,5 +32,5 @@ using System.Runtime.InteropServices;
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.1.0.0")]
[assembly: AssemblyFileVersion("1.1.0.0")]
[assembly: AssemblyVersion("2.0.0.0")]
[assembly: AssemblyFileVersion("2.0.0.0")]

46
Properties/Settings.Designer.cs generated Normal file
View File

@ -0,0 +1,46 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:4.0.30319.42000
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace LocalExplorer.Properties {
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "16.10.0.0")]
internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase {
private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings())));
public static Settings Default {
get {
return defaultInstance;
}
}
[global::System.Configuration.ApplicationScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Configuration.DefaultSettingValueAttribute("ssv.wa.lcl\\")]
public string PREFIX {
get {
return ((string)(this["PREFIX"]));
}
}
[global::System.Configuration.ApplicationScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Configuration.DefaultSettingValueAttribute("<?xml version=\"1.0\" encoding=\"utf-16\"?>\r\n<ArrayOfString xmlns:xsi=\"http://www.w3." +
"org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">\r\n <s" +
"tring>.docx</string>\r\n <string>.pdf</string>\r\n</ArrayOfString>")]
public global::System.Collections.Specialized.StringCollection ALLOWED_EXTENSIONS {
get {
return ((global::System.Collections.Specialized.StringCollection)(this["ALLOWED_EXTENSIONS"]));
}
}
}
}

View File

@ -0,0 +1,16 @@
<?xml version='1.0' encoding='utf-8'?>
<SettingsFile xmlns="http://schemas.microsoft.com/VisualStudio/2004/01/settings" CurrentProfile="(Default)" GeneratedClassNamespace="LocalExplorer.Properties" GeneratedClassName="Settings">
<Profiles />
<Settings>
<Setting Name="PREFIX" Type="System.String" Scope="Application">
<Value Profile="(Default)">ssv.wa.lcl\</Value>
</Setting>
<Setting Name="ALLOWED_EXTENSIONS" Type="System.Collections.Specialized.StringCollection" Scope="Application">
<Value Profile="(Default)">&lt;?xml version="1.0" encoding="utf-16"?&gt;
&lt;ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;
&lt;string&gt;.docx&lt;/string&gt;
&lt;string&gt;.pdf&lt;/string&gt;
&lt;/ArrayOfString&gt;</Value>
</Setting>
</Settings>
</SettingsFile>