【发布时间】:2010-11-27 16:36:48
【问题描述】:
我正在更新我们的一个应用程序。它必须使用 .NET 2.0。一部分使用
在桌面上创建文件FileStream fs = new FileStream(Environment.GetFolderPath
(Environment.SpecialFolder.DesktopDirectory), FileMode.Create);
但是我在 Windows 7 中遇到了 UnauthorizedAccessException(我假设也是 Vista,尽管我还没有测试过)。我查看了海拔(不是针对整个程序,而是针对将创建文件并对其执行操作的单独程序集);但是,这似乎需要 .NET 3.0 或 3.5。有没有办法使用 .NET 2.0 访问桌面文件夹? (要求程序以管理员身份运行也不是一种选择)
(我进行了搜索,唯一与我要问的问题接近的问题是:File creation fails in standard account (Vista) 但是它正在谈论提升整个应用程序并且不是特定于 .NET 2.0,所以我相信这不是重复)
编辑:
哇,我真的很笨。这实际上工作正常。我试图创建一个名为 C:\Users\MyUser\Desktop 的文件。哎呀。很抱歉造成混乱。
编辑:这是异常的文本:
System.UnauthorizedAccessException was unhandled
Message="Access to the path 'C:\\Users\\MyUser\\Desktop' 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)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy)
at System.IO.FileStream..ctor(String path, FileMode mode)
at MyProgram.Prog.SaveDiagnostic(String filename, String text) in C:\Source\MyProgram\Prog.cs:line 95
at MyProgram.Form1.buttonGenDiagnostic_Click(Object sender, EventArgs e) in C:\Source\MyProgram\Form1.cs:line 4729
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(Int32 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 Northwoods.CRM.Import.Form1.Main(String[] args) in I:\WebProspect\Source\Northwoods.CRM.Import\Form1.cs:line 2616
at System.AppDomain._nExecuteAssembly(Assembly 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.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
【问题讨论】:
-
我认为您应该发布完整的错误 exception.ToString() 并带有完整的跟踪,因为这听起来与管理员帐户无关。 Environment.GetFolderPath 将始终为正在运行程序的当前用户返回正确的桌面,并且当前用户始终具有对桌面的完全访问权限以在桌面路径上创建/删除文件。但是,我们在 vista 上为非管理员帐户执行此操作,它确实有效,对于 Windows 7,我将很快进行测试,但我仍然坚持您发布详细的堆栈跟踪。
-
它确实为当前用户获取了正确的桌面文件夹,但我猜 Win7 不允许程序在那里修改任何内容。不过,我仍然会发布完整的例外情况。
-
@Nick:具有讽刺意味的是,当我试图重现您的问题时,我产生了完全相同的异常。我没有复制并粘贴您的代码,但我写了类似的东西并得到了异常,然后通过在路径中添加一个实际的文件名来更改它以使其工作。我从来没有注意到你的代码做了同样的事情。我通常会尝试在我的代码示例中放置一个换行符,这样我就不会得到水平滚动条 - 否则更容易错过这样的事情。
-
@Nick:我刚刚注意到,当我编辑您的问题时,您也曾尝试插入换行符,但它无法正常工作。这个编辑器很奇怪。
-
你真的应该接受 Orion 的答案,因为他的答案比你自己的更清晰......在应得的地方给予表扬等等。
标签: c# windows-vista permissions uac