unauthorizedaccessexception

Hello All:
I am trying to write a log string from a C# program to a network share folders, unfortunately when I try to do this they give me a unauthorizedaccessexception. My code looks like below

FileStream newfile = new FileStream(@"\\shareserver\Attachments\test.txt", FileMode.CreateNew, FileAccess.ReadWrite);
            TextWriter tw = new StreamWriter(newfile);
            tw.Write("testing");
            tw.Close();

now if I just put \\shareserver\Attachments in the browser, the directory pops up. I have full administration server rights to this folder and server. So why the exception? Can someone help please!

Thank You!
pengbsamAsked:
Who is Participating?
 
Tommie Nathaniel Carter, Jr., MBAFounderCommented:
The exception is happening because the code accessing the folder is not trusted. You can change this using the caspol utility.

An article explaining how CASPol works;
http://msdn.microsoft.com/msdnmag/issues/05/11/CodeAccessSecurity/default.aspx?loc=&side=true

Check out the section entitled 'To grant full trust to the assembly and the document'
http://msdn2.microsoft.com/en-us/library/ms404837(VS.80).aspx

Caspol tips:
http://www.dotnetspider.com/kb/Article413.aspx
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.