Reputation: 101
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:
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?
Upvotes: 1
Views: 20354
Reputation: 1
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"
.
Upvotes: 0
Reputation: 16056
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
Upvotes: 1
Reputation: 31184
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.
Upvotes: 1