From 67f003b59364a0ac40f380c55fd4f212bdd05a5b Mon Sep 17 00:00:00 2001 From: Adrian Hum Date: Mon, 7 Dec 2015 14:03:09 +1100 Subject: [PATCH] Update Main.cs Just had a moment to add some resolution to your try/catch block --- Main.cs | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/Main.cs b/Main.cs index f820c8d..66a8e21 100644 --- a/Main.cs +++ b/Main.cs @@ -67,9 +67,29 @@ static void Main(string[] args) private static void SetReadOnly(string fileName, bool toBeReadOnly) { - var attr = File.GetAttributes(fileName); - attr = toBeReadOnly ? attr | FileAttributes.ReadOnly : attr & ~FileAttributes.ReadOnly; - File.SetAttributes(fileName, attr); + try { + var attr = File.GetAttributes(fileName); + attr = toBeReadOnly ? attr | FileAttributes.ReadOnly : attr & ~FileAttributes.ReadOnly; + File.SetAttributes(fileName, attr); + } + catch(Exception ex) + { + if (ex is ArgumentException || + ex is PathTooLongException || + ex is PathTooLongException || + ex is DirectoryNotFoundException || + ex isFileNotFoundException) + { + //Something is wrong with the file... + throw ex; + } + if (ex is NotSupportedException || + ex is UnauthorizedAccessException) + { + //You have a security or implementation problem + throw ex; + } + } } @@ -122,4 +142,4 @@ private static string ProcessLinePart(string line, string part) { return line; } } -} \ No newline at end of file +}