1

Morning,

I am trying to create some text files to a folder on my desktop but am getting an a message saying “UnauthorizedAccessException was unhandled” error, Access to the path ‘C:\MSGTEST’ is denied.

I don’t understand why I am getting this message as:

  • I have full access to this directory.
  • UAC is turned off.
  • I am a local administrator.

Can someone tell me why this is happening?

The program bombs on the below line:

using (FileStream MSGFiles = new FileStream(MSGRootPath, FileMode.OpenOrCreate, FileAccess.Write))

My code to write the text files to the directory:

using (FileStream MSGFiles = new FileStream(MSGRootPath, FileMode.OpenOrCreate, FileAccess.Write))
    using (StreamWriter sw = new StreamWriter(MSGFiles))
    {

        while (readerObj.Read())
        {
            SqlMsg = readerObj["Msg"].ToString();
            SqlRef = readerObj["Reference"].ToString();
            SqlType = readerObj["Type"].ToString();
            //SqlTime = readerObj["Time"].ToString();

            //SqlTime = SqlTime.Replace(":", "-");
            //SqlTime = SqlTime.Replace("/", "-");

            sw.WriteLine(SqlRef + "_" + SqlType + ".txt");

Exception error:

System.UnauthorizedAccessException was unhandled HResult=-2147024891 Message=Access to the path 'C:\MSGTEST' is denied. Source=mscorlib StackTrace: at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost) at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access) at SwiftMSGDownload.Form1.subGetMSGs() in c:\Users\nasswee\Documents\Visual Studio 2012\Projects\SwiftMSGDownload\SwiftMSGDownload\Form1.cs:line 47 at SwiftMSGDownload.Form1.btnStart_Click(Object sender, EventArgs e) in c:\Users\nasswee\Documents\Visual Studio 2012\Projects\SwiftMSGDownload\SwiftMSGDownload\Form1.cs:line 99 at System.Windows.Forms.Control.OnClick(EventArgs e) at System.Windows.Forms.Button.OnClick(EventArgs e) at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.ButtonBase.WndProc(Message& m) at System.Windows.Forms.Button.WndProc(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData) at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.Run(Form mainForm) at SwiftMSGDownload.Program.Main() in c:\Users\nasswee\Documents\Visual Studio 2012\Projects\SwiftMSGDownload\SwiftMSGDownload\Program.cs:line 18 at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() at System.Threading.ThreadHelper.ThreadStart_Context(Object state) at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart() InnerException:

Any ideas?

Mong Zhu
  • 23,309
  • 10
  • 44
  • 76
Andrea
  • 101
  • 1
  • 2
  • 13

3 Answers3

1

Even though you are a local administrator, you might not be running your program "as an administrator"

Right click on Visual studio, and select "run as administrator", if you want to debug your program as an administrator.

1

the quickest solution for this issue is to allow full permissions to everybody to this path "C:\MSGTEST"(this is a major security concern) the correct approach would be to allow persmisions onle to IUSR or IIS_IUSERS depending on your IIS version

take a look to the following link

Community
  • 1
  • 1
pedrommuller
  • 15,741
  • 10
  • 76
  • 126
  • Tried this of giving full permssions to everyone on that folder but still getting same error. – Andrea Feb 04 '14 at 17:14
  • BTW i'm not using IIS for this program. – Andrea Feb 04 '14 at 17:15
  • Is there any chance that another process is taking ownership of the folder avoiding you to have access to it? – pedrommuller Feb 04 '14 at 19:42
  • No I dont think so. Whats interesting is that I tried a different approach by using this: using (FileStream MSGFiles = new FileStream(@"\\123.456.78.9\msgtest\test.txt", FileMode.OpenOrCreate)) instead of the line i posted above. This created a test.txt file in the folder and within the file it wrote my filenames. But i want it to create the file names within the directory not within a text file. Understand? – Andrea Feb 04 '14 at 21:26
  • Working Fine :) – Thulasiram Aug 20 '19 at 10:23
0

I am not sure exactly where MSGRootPath points to, but I suspect it contains the value "C:\MSGTEST" from the error report. If this is the case, you are getting this exception because Windows does not allow you to write to a folder directly like you appear to attempt. (As in, opening folders for writing is impossible, I have no idea why they decided to make it throw an UnauthorizedAccessException through)

After reading your comments on another answer, this might work instead: open file streams inside the while loop instead with the name MSGRootPath + SqlRef + "_" + SqlType + ".txt".

Fayti1703
  • 1
  • 1