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

标签 c# windows-vista permissions uac

我正在更新我们的一个应用程序。它必须使用 .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: 

最佳答案

问题出在这段代码

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,或处理您的异常 - 例如代码运行两次,并且文件在第二次尝试时已经存在,因此您不会能够第二次创建它

关于c# - 在 .NET 2.0 中将文件保存到 Vista/Windows 7 中的桌面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1414000/

相关文章:

c# - 是否可以在 Monotouch 中使用 NSZombieEnabled?

c# - 值类型如何实现引用类型

delphi - 自定义 Windows Vista 日历显示

Apache 服务器中的 Python exec() 权限被拒绝 :/cgi_bin/*. py

android - 为什么在 Android 应用程序中使用 gmail 登录时显示未注册的 Android 应用程序消息?

python - Django 守护者 : How to give all users the permissions of the anonymous user

c# - 如何在数据表的组合框中显示多个值

c# - 如何以只读方式传递字节数组?

c++ - 防止在 Windows 中取消停靠计算机

database - 在 Windows 安全模式下运行 SQL Server 2005