在.NET 2.0中的Vista/Windows 7中将文件保存到桌面

Nic*_*win 5 c# permissions uac windows-vista

我正在努力更新我们的一个应用程序.它必须使用.NET 2.0.一部分使用桌面创建文件

FileStream fs = new FileStream(Environment.GetFolderPath
    (Environment.SpecialFolder.DesktopDirectory), FileMode.Create);
Run Code Online (Sandbox Code Playgroud)

但是我在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: 
Run Code Online (Sandbox Code Playgroud)

Ori*_*rds 14

问题在于此代码

FileStream fs = new FileStream(Environment.GetFolderPath
    (Environment.SpecialFolder.DesktopDirectory), FileMode.Create);
Run Code Online (Sandbox Code Playgroud)

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

var desktopFolder = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
var fs = new FileStream(desktopFolder, FileMode.Create);
Run Code Online (Sandbox Code Playgroud)

你在这里尝试做的不是在桌面上创建文件,而是试图创建桌面文件夹本身.桌面文件夹显然已经存在,因此您收到错误.

什么,你需要做的就是创建一个文件的桌面文件夹.您可以使用Path.Combine这样做,如下所示:

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

您可能还希望将FileMode更改为OpenOrCreate或处理异常 - 例如,代码运行两次,并且该文件将在第二次尝试时已存在,因此您将无法再次创建它