Check your current permissions before writing to a directory

File permissions

To avoid exceptions is nice to check the current permissions before saving or writing to a file in a directory.

Example of checkpermissions method:

       private bool  CheckWritePermission(string filepath) 
        {
            bool CanWrite = false;
            FileIOPermission fileIOpermission = new FileIOPermission(FileIOPermissionAccess.Write,filepath);
            if (System.Security.SecurityManager.IsGranted(fileIOpermission))
            {
                CanWrite = true;
            }
            return CanWrite;
        }

Unit test

Made a unit test for test. It's just testing c:\temp maybe it's a good idea to test multiple directory's

        /// 
        ///A test for CheckWritePermission
        ///
        [TestMethod()]
        public void CheckWritePermissionTest()
        {
            FileBase target = new FileBase();
            string filepath = @"c:\temp";
            bool expected = true; 
            bool actual;
            actual = target.CheckWritePermission(filepath);
            Assert.AreEqual(expected, actual);
        }

{{opmerking.Naam}}:

{{opmerking.OpmerkingText}}

            

Saving your comment....

Naam is verplicht!
Email is verplicht!
Opmerking is verplicht!