C# 拒绝访问以写入文件夹,为什么?
早上好 我试图在桌面上的文件夹中创建一些文本文件,但收到一条消息说“UnauthorizedAccessException未处理”错误,对路径“C:\MSGTEST”的访问被拒绝 我不明白为什么我会收到这样的信息:C# 拒绝访问以写入文件夹,为什么?,c#,filestream,access-denied,unauthorizedaccessexcepti,C#,Filestream,Access Denied,Unauthorizedaccessexcepti,早上好 我试图在桌面上的文件夹中创建一些文本文件,但收到一条消息说“UnauthorizedAccessException未处理”错误,对路径“C:\MSGTEST”的访问被拒绝 我不明白为什么我会收到这样的信息: 我可以完全访问这个目录 UAC已关闭 我是本地管理员 有人能告诉我为什么会这样吗 该程序在下面的行中爆炸: using (FileStream MSGFiles = new FileStream(MSGRootPath, FileMode.OpenOrCreate, FileAc
- 我可以完全访问这个目录
- UAC已关闭
- 我是本地管理员
using (FileStream MSGFiles = new FileStream(MSGRootPath, FileMode.OpenOrCreate, FileAccess.Write))
将文本文件写入目录的代码:
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");
异常错误:
System.UnauthorizedAccessException未处理
HResult=-2147024891
Message=对路径“C:\MSGTEST”的访问被拒绝。
Source=mscorlib
堆栈跟踪:
在System.IO.\uuu Error.WinIOError(Int32 errorCode,字符串maybeFullPath)
在System.IO.FileStream.Init(字符串路径、文件模式、文件访问权限、Int32权限、布尔用户权限、文件共享、Int32缓冲大小、文件选项选项、安全属性secAttrs、字符串msgPath、布尔bFromProxy、布尔useLongPath、布尔checkHost)
位于System.IO.FileStream..ctor(字符串路径、文件模式、文件访问)
在c:\Users\nasswee\Documents\Visual Studio 2012\Projects\SwiftMSGDownload\SwiftMSGDownload\Form1.cs中的SwiftMSGDownload.Form1.subGetMSGs()处:第47行
在SwiftMSGDownload.Form1.btnStart\u中,单击c:\Users\nasswee\Documents\Visual Studio 2012\Projects\SwiftMSGDownload\SwiftMSGDownload\Form1.cs中的(对象发送方,事件参数e):第99行
在System.Windows.Forms.Control.OnClick(EventArgs e)中
在System.Windows.Forms.Button.OnClick(EventArgs e)中
在System.Windows.Forms.Button.OnMouseUp(MouseEventArgs-mevent)上
在System.Windows.Forms.Control.WmMouseUp(Message&m、MouseButtons按钮、Int32单击)
位于System.Windows.Forms.Control.WndProc(Message&m)
位于System.Windows.Forms.ButtonBase.WndProc(Message&m)
在System.Windows.Forms.Button.WndProc(Message&m)中
在System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&m)中
在System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&m)中
在System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd、Int32 msg、IntPtr wparam、IntPtr lparam)
在System.Windows.Forms.UnsafentiveMethods.DispatchMessageW(MSG&MSG)中
位于System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafentiveMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID、Int32 reason、Int32 pvLoopData)
位于System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32原因,ApplicationContext上下文)
位于System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32原因,ApplicationContext上下文)
在System.Windows.Forms.Application.Run(Form mainForm)中
在c:\Users\nasswee\Documents\Visual Studio 2012\Projects\SwiftMSGDownload\SwiftMSGDownload\Program.cs中的SwiftMSGDownload.Program.Main()处:第18行
位于System.AppDomain.\u nExecuteAssembly(RuntimeAssembly程序集,字符串[]args)
位于System.AppDomain.ExecuteAssembly(字符串汇编文件、证据汇编安全性、字符串[]args)
在Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()上
位于System.Threading.ThreadHelper.ThreadStart\u上下文(对象状态)
位于System.Threading.ExecutionContext.RunInternal(ExecutionContext ExecutionContext、ContextCallback回调、对象状态、布尔值preserveSyncCtx)
在System.Threading.ExecutionContext.Run(ExecutionContext ExecutionContext,ContextCallback回调,对象状态,布尔保存SyncCTX)
在System.Threading.ExecutionContext.Run(ExecutionContext ExecutionContext,ContextCallback回调,对象状态)
位于System.Threading.ThreadHelper.ThreadStart()处
内部异常:
有什么想法吗?即使您是本地管理员,也可能不是“以管理员身份”运行您的程序
如果要以管理员身份调试程序,请右键单击Visual studio,然后选择“以管理员身份运行”。此问题的最快解决方案是允许每个人对路径“C:\MSGTEST”拥有完全权限(这是一个主要的安全问题)正确的方法是根据您的IIS版本允许个人仅访问IUSR或IIS用户
查看以下内容我不确定MsgRotPath指向的确切位置,但我怀疑它包含错误报告中的值“C:\MSGTEST”。 如果是这种情况,则会出现此异常,因为Windows不允许您像您似乎尝试的那样直接写入文件夹。(就像在中一样,打开文件夹进行写作是不可能的,我不知道为什么他们决定让它抛出一个UnauthorizedAccessException)
在阅读了您对另一个答案的评论后,这可能会起作用:在while循环中打开名为
MSGRootPath+SqlRef+““+SqlType+”.txt“
的文件流,而不是使用根文件夹来对抗系统。使用Environment.SpecialFolder
名称之一。当您以管理员身份运行它时,它是否工作?Environment.SpecialFolder不是一个选项,因为最终目标将是服务器共享。但是,当我指向共享时,我遇到了相同的错误。以管理员身份运行VS仍然会出现相同的错误。尝试过对该文件夹上的所有人进行完全授权,但仍然遇到相同的错误。顺便说一句,我没有为此程序使用IIS。是否有可能另一个进程占用了该文件夹的所有权,从而使您无法访问是吗?不,我不这么认为。有趣的是,我尝试了一种不同的方法:使用(FileStream MSGFiles=new FileStream(@“\\123.456.78.9\msgtest\test.txt”,FileMode.OpenOrCreate))而不是我上面发布的行。这在th中创建了一个test.txt文件