UnauthorizedAccessException

  • Thread starter Peter Morris [Droopy eyes software]
  • Start date
P

Peter Morris [Droopy eyes software]

Why on Earth am I suddenly getting this exception when it has been working
for the past 3 months without problems? The line of code that throws the
exception is....

FileStream xmlStream = new FileStream("\\Program
Files\\MyAppFolder\\DataImport.xml", FileMode.Open);


The exception information is.....

System.UnauthorizedAccessException was unhandled
Message="UnauthorizedAccessException"
StackTrace:
at System.IO.__Error.WinIOError()
at System.IO.FileStream..ctor()
at System.IO.FileStream..ctor()
at ......SyncWithServerTask.ImportData()
at ......SyncWithServerTask.SyncWithServer()
at ......SyncWithServerTask.set_CurrentState()
at ......SyncWithServerTask.Next()
at ......MainForm.NextButton_Click()
at System.Windows.Forms.Control.OnClick()
at System.Windows.Forms.Button.OnClick()
at System.Windows.Forms.ButtonBase.WnProc()
at System.Windows.Forms.Control._InternalWnProc()
at Microsoft.AGL.Forms.EVL.EnterModalDialog()
at System.Windows.Forms.Form.ShowDialog()
at ......TaskExecutor.Execute()
at ......Main()


Any help would be greatly appreciated, thanks!

Pete
 
P

Peter Morris [Droopy eyes software]

Okay, I can add an additional parameter "FileAccess.Read" at the end but at
some point I am going to want to delete this file too, so can I expect this
error again and what could be causing it?
 
S

Sergey Bogdanov

I assume DataImport.xml has read only attribute. When you wrote
FileStream("\\Program
Files\\MyAppFolder\\DataImport.xml", FileMode.Open)

it indeed means:

FileStream("\\Program Files\\MyAppFolder\\DataImport.xml",
FileMode.Open, FileAccess.ReadWrite...)

You can't get write access to readonly files - thus you got this exception.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top