在.NET 2.0的Vista/Windows 7中将文件保存到桌面 [英] Save File to Desktop in Vista/Windows 7 in .NET 2.0

查看:76
本文介绍了在.NET 2.0的Vista/Windows 7中将文件保存到桌面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在更新我们的一个应用程序.它必须使用.NET 2.0.一部分使用

I'm working on updating one of our applications. It must use .NET 2.0. One portion creates a file on the Desktop using

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

但是我在Windows 7中得到了UnauthorizedAccessException(我还假设还有Vista,尽管我还没有对此进行测试).我研究了仰角(不是针对整个程序,而是针对一个单独的程序集,它将创建文件并对其执行操作);但是,这似乎需要.NET 3.0或3.5.有什么方法可以使用.NET 2.0来访问桌面"文件夹? (也不要求以管理员身份运行该程序)

But I get an UnauthorizedAccessException in Windows 7 (and Vista too, I'm assuming, though I haven't tested that yet). I looked into elevation (not for the entire program, but for a separate assembly which would create the file and perform actions on it); however that seems to require .NET 3.0 or 3.5. Is there any way to gain access to the Desktop folder using .NET 2.0? (Requiring the program to be run as an Administrator is also not an option)

(我进行了搜索,唯一接近我要问的问题是:

(I did a search, and the only question close to what I'm asking is this: File creation fails in standard account (Vista) however it is talking about elevating the entire app and is not .NET 2.0 specific, so I believe this isn't a duplicate)


哇,我真傻.这实际上很好.我试图创建一个名为C:\ Users \ MyUser \ Desktop的文件.哎呀.抱歉造成混乱.


Wow, I was being really dumb. This actually works fine. I was trying to create a file called C:\Users\MyUser\Desktop. Oops. Sorry for the confusion.

这是例外的文字:

  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);

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

Let's rewrite it into the steps that actually will occur

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

您要在此处执行的操作不是在桌面上创建文件,而是在尝试创建桌面文件夹本身.桌面文件夹显然已经存在,所以会出现错误.

What you're trying to do here is not create a file on the desktop, you are trying to create the desktop folder itself. The desktop folder obviously already exists, so you get an error.

您需要做的是在桌面文件夹内 中创建一个文件.您可以使用Path.Combine来做到这一点,就像这样:

What you need to do is create a file inside the desktop folder. You can use Path.Combine to do this, like this:

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

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

You may also want to change the FileMode to OpenOrCreate, or handle your exceptions - if for example the code runs twice, and the file will already exist on the second try, so you won't be able to create it a second time

这篇关于在.NET 2.0的Vista/Windows 7中将文件保存到桌面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆