Merge pull request #265 from Microsoft/fix-accessdenied-write

Wrap file system export write in try/catch.
This commit is contained in:
Michael Niksa 2018-10-02 10:49:38 -07:00 committed by GitHub
commit e5aa14ea7b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -297,9 +297,9 @@ namespace ColorTool
Console.Write("\x1b[m");
}
private static IntPtr GetStdOutputHandle()
{
return GetStdHandle(STD_OUTPUT_HANDLE);
private static IntPtr GetStdOutputHandle()
{
return GetStdHandle(STD_OUTPUT_HANDLE);
}
static void PrintSchemes()
@ -433,21 +433,29 @@ namespace ColorTool
bool success = GetConsoleScreenBufferInfoEx(hOut, ref csbiex);
if (success)
{
using (System.IO.StreamWriter file = new System.IO.StreamWriter(outputPath))
try
{
file.WriteLine("[table]");
for (int i = 0; i < 16; i++)
// StreamWriter can fail for a variety of file system reasons so catch exceptions and print message.
using (System.IO.StreamWriter file = new System.IO.StreamWriter(outputPath))
{
string line = IniSchemeParser.COLOR_NAMES[i];
line += " = ";
uint color = csbiex.ColorTable[i];
uint r = color & (0x000000ff);
uint g = (color & (0x0000ff00)) >> 8;
uint b = (color & (0x00ff0000)) >> 16;
line += r + "," + g + "," + b;
file.WriteLine(line);
file.WriteLine("[table]");
for (int i = 0; i < 16; i++)
{
string line = IniSchemeParser.COLOR_NAMES[i];
line += " = ";
uint color = csbiex.ColorTable[i];
uint r = color & (0x000000ff);
uint g = (color & (0x0000ff00)) >> 8;
uint b = (color & (0x00ff0000)) >> 16;
line += r + "," + g + "," + b;
file.WriteLine(line);
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
else
{