Nant – How to check that the user has write access to a particular folder.

nant

I want to test that my script has access to a particular file share using Nant. So I started checking this by doing a simple file copy onto that share. This worked fine, but when the share does not have write permissions, the script is crashing, even though I have a try-catch in place, with the error:

System.UnauthorizedAccessException: Access to the path '\\mypc\testsharefolder\systemaccesscheck.txt' is denied.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)*

Kindly suggest to me a way to get around this problem, or suggest script to check the access rights.

`<target name="test.target">
 <echo>start of try catch block</echo>
<trycatch>
<try>
<copy file="c:\systemaccesscheck.txt" todir="\\mypc\testsharefolder" overwrite="true"/>
<echo>filecopy passed</echo>
</try>
<catch property="failure">
<echo>inside catch block because of failure</echo>
</catch>
</trycatch>
<echo>End of try catch block</echo>`

Best Answer

Find out which user account is used to run the NAnt scripts, and then check that user account has permissions on the target folder that you're trying to copy to.

You could even try to log onto your PC using that account and manually trying to create a a new text file in the target folder.

Hope this helps.

Related Topic