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?
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.