[BUG] Resolve network root sometimes not working with WARN ArgumentOutOfRangeException: Length cannot be less than zero. (Parameter 'length') #129

This commit is contained in:
Markus Hofknecht 2020-09-26 15:54:24 +02:00
parent cbdef123ba
commit 49c6ef05f0
5 changed files with 931 additions and 903 deletions

View file

@ -284,8 +284,6 @@ namespace SystemTrayMenu.Business
static string[] GetDirectoriesInNetworkLocation(string networkLocationRootPath)
{
List<string> directories = new List<string>();
try
{
Process cmd = new Process();
cmd.StartInfo.FileName = "cmd.exe";
cmd.StartInfo.RedirectStandardInput = true;
@ -303,25 +301,32 @@ namespace SystemTrayMenu.Business
cmd.WaitForExit();
cmd.Close();
output = output.Substring(output.LastIndexOf('-') + 2);
output = output.Substring(0, output.IndexOf("The command completed successfully.", StringComparison.InvariantCulture));
bool resolvedSomething = false;
foreach (string line in output
.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries))
List<string> lines = output
.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries).ToList();
if (lines.Count > 8)
{
int indexOfSecondColumnTypeDisk = line.IndexOf("Disk", StringComparison.InvariantCulture);
if (indexOfSecondColumnTypeDisk > 0)
foreach (string line in lines.Skip(6).SkipLast(2))
{
int indexOfFirstSpace = line.IndexOf(" ", StringComparison.InvariantCulture);
if (indexOfFirstSpace > 0)
{
string directory = Path.Combine(
networkLocationRootPath,
line.Substring(0, indexOfSecondColumnTypeDisk));
directories.Add(directory);
}
}
}
catch (ArgumentOutOfRangeException ex)
line.Substring(0, indexOfFirstSpace));
if (Directory.Exists(directory))
{
Log.Warn($"Could not resolve network root folder: {networkLocationRootPath}", ex);
directories.Add(directory);
resolvedSomething = true;
}
}
}
}
if (!resolvedSomething)
{
Log.Info($"Could not resolve network root folder: {networkLocationRootPath} , output:{output}");
}
return directories.ToArray();

View file

@ -9,7 +9,7 @@
<Identity
Name="49543SystemTrayMenu.SystemTrayMenu"
Publisher="CN=5884501C-92ED-45DE-9508-9D987C314243"
Version="1.0.15.0" />
Version="1.0.16.0" />
<Properties>
<DisplayName>SystemTrayMenu</DisplayName>

View file

@ -39,5 +39,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.0.16.0")]
[assembly: AssemblyFileVersion("1.0.16.0")]
[assembly: AssemblyVersion("1.0.16.1")]
[assembly: AssemblyFileVersion("1.0.16.1")]

View file

@ -61,6 +61,8 @@ namespace SystemTrayMenu.Utilities
{
bool isDirectoryToHide = false;
if (path.Length < 260)
{
try
{
FileAttributes attributes = File.GetAttributes(path);
hiddenEntry = attributes.HasFlag(FileAttributes.Hidden);
@ -72,6 +74,15 @@ namespace SystemTrayMenu.Utilities
isDirectoryToHide = true;
}
}
catch (UnauthorizedAccessException ex)
{
Log.Warn($"path:'{path}'", ex);
}
}
else
{
Log.Info($"path too long (>=260):'{path}'");
}
return isDirectoryToHide;
}

View file

@ -5,6 +5,7 @@
namespace SystemTrayMenu.Utilities
{
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
@ -15,6 +16,8 @@ namespace SystemTrayMenu.Utilities
internal static class Log
{
private static readonly Logger LogValue = new Logger(string.Empty);
private static List<string> warnings = new List<string>();
private static List<string> infos = new List<string>();
internal static void Initialize()
{
@ -22,13 +25,22 @@ namespace SystemTrayMenu.Utilities
}
internal static void Info(string message)
{
if (!infos.Contains(message))
{
LogValue.Info(message);
infos.Add(message);
}
}
internal static void Warn(string message, Exception ex)
{
LogValue.Warn($"{message}{Environment.NewLine}{ex}");
string warning = $"{message} {ex.ToString().Replace(Environment.NewLine, " ", StringComparison.InvariantCulture)}";
if (!warnings.Contains(warning))
{
LogValue.Warn(warning);
warnings.Add(warning);
}
}
internal static void Error(string message, Exception ex)