我正在努力更新我们的一个应用程序。它必须使用.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访问Desktop文件夹? (要求以管理员身份运行程序也不是一种选择)

(我做了一次搜索,唯一接近我问的问题是:标准帐户(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: 
有帮助吗?

解决方案

问题在于此代码

FileStream fs = new FileStream(Environment.GetFolderPath
    (Environment.SpecialFolder.DesktopDirectory), FileMode.Create);

让我们将其重写为实际发生的步骤

var desktopFolder = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
var fs = new FileStream(desktopFolder, FileMode.Create);

您在此处尝试执行的操作不是在桌面上创建文件,而是尝试创建桌面文件夹。桌面文件夹显然已经存在,因此您收到错误。

您需要做的是在桌面文件夹中创建文件 。您可以使用 Path.Combine 来执行此操作,如下所示:

var desktopFolder = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
var fullFileName = Path.Combine(desktopFolder, "Test.txt");
var fs = new FileStream(fullFileName, FileMode.Create);

您可能还想将FileMode更改为 OpenOrCreate ,或者处理您的异常 - 例如,如果代码运行两次,并且该文件在第二次尝试时已经存在,那么您将不会能够再次创建它

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top