简体   繁体   English

为什么我的C#app无法在Windows 7中创建文件?

[英]Why can't my C# app create files in Windows 7?

I have a C# app that creates a settings file for itself to store the current state of certain visual elements. 我有一个C#应用程序,它为自己创建一个设置文件来存储某些可视元素的当前状态。 This app works just fine on any machine that isn't running Windows 7, but on those machines we get an error that the settings file can't be created because the user doesn't have permission. 此应用程序适用于任何未运行Windows 7的计算机,但在这些计算机上,我们收到错误,因为用户没有权限,无法创建设置文件。 Now, I could fix this issue by going to each computer, logging in as the administrator and giving the user read and write access on the program folder for the application that we've installed, but there has to be a better way. 现在,我可以通过访问每台计算机,以管理员身份登录并为用户提供对我们已安装的应用程序的程序文件夹的读写访问权来解决此问题,但必须有更好的方法。

It seems like in XP, you got write access on the folders you created by default, but that isn't the case anymore. 看起来在XP中,您对默认创建的文件夹具有写入权限,但情况不再如此。 Is there a setting I need in the setup package to make this work? 我需要在安装程序包中进行设置才能使其正常工作吗?

The point is that you shouldn't be storing settings files in the program folder. 关键是您不应该将设置文件存储在程序文件夹中。 Microsoft have advised against this for a long time, but started making things stricter with Vista IIRC. 微软长期以来一直反对这一建议,但开始对Vista IIRC采取更严格的措施。

Use Environment.SpecialFolders.ApplicationData (etc) to find the most appropriate place to put settings. 使用Environment.SpecialFolders.ApplicationData (etc)查找最适合放置设置的位置。 Or use the .NET settings infrastructure which does this automatically for you. 或者使用.NET设置基础结构,它会自动为您执行此操作。

are you trying to create files in the installation folder? 你想在安装文件夹中创建文件吗? you should be using the user data folder for data and not the installation folders. 您应该使用用户数据文件夹来存储数据而不是安装文件夹。 Use the Environment.SpecialFolders.ApplicationData folder to get a folder you can write to. 使用Environment.SpecialFolders.ApplicationData文件夹获取可以写入的文件夹。

You're probably running as an administrator on your non-Windows 7 machine which an write anywhere. 您可能在非Windows 7计算机上以管理员身份运行,可以随处写入。 Be sure to save any per user instance data in their AppData folder (roaming if it should follow them from computer to computer, or local if its a cache or local to taht machine only). 请确保将每个用户实例数据保存在其AppData文件夹中(如果它应该从计算机跟随它们进行漫游,或者如果它只是缓存或本地到taht机器,则为本地)。 If you need to share settings between users, use the C:\\ProgramData folder with the appropriate permissions. 如果需要在用户之间共享设置,请使用具有相应权限的C:\\ ProgramData文件夹。

A program shouldn't try to store settings in its installation directory. 程序不应尝试将设置存储在其安装目录中。

Be sure to use the SpecialFolders along with Environment.GetFolderPath to get the right locations needed. 请务必使用SpecialFoldersEnvironment.GetFolderPath来获取所需的正确位置。 You should never hard code paths because they can be different between versions AND languages. 您永远不应该硬编码路径,因为它们在版本和语言之间可能不同。 (I know in the German version of XP it wasn't Program Files but Programme !) (我知道在XP的德语版本中它不是Program Files而是Programme !)

this app works just fine on any machine that isn't running Windows 7 这个应用程序适用于任何未运行Windows 7的计算机

Wrong! 错误! It only works on those machines if you run as administrator . 如果您以管理员身份运行,它仅适用于这些计算机。 I think you'll find your program is broken on Windows XP as well if you try to run it on just about any business computer rather than a home computer. 我想如果你尝试在任何商用计算机而不是家用计算机上运行它,你会发现你的程序在Windows XP上也被破坏了。

Instead, this kind of information needs to go in one of the special Application Data folders. 相反,这种信息需要进入一个特殊的Application Data文件夹。

This is a security flaw in your program because your program is writing information to the program directory (which is, and should be, protected.) If it's a situation of correcting the root cause, consider using the SpecialFolder enumeration or the static members on Application like like CommonAppDataPath to write your information to a more appropriate location. 这是程序中的安全漏洞,因为您的程序正在将信息写入程序目录(这应该受到保护。)如果是纠正根本原因的情况,请考虑使用SpecialFolder枚举Application上的静态成员像CommonAppDataPath一样将您的信息写入更合适的位置。

Assuming the typical approach to writing a file via a path, this is a trivial fix and there's no good "expediency" reason to not correct the root cause. 假设通过路径编写文件的典型方法,这是一个微不足道的修复,没有好的“权宜之计”理由不能纠正根本原因。 If you're not sure about how to manipulate the path, consider using Path.Combine() . 如果您不确定如何操作路径,请考虑使用Path.Combine() It does it for you. 它为你做到了。

In general, you shouldn't be writing program data to any folder underneath Program Files (even if you created the folder). 通常,您不应该将程序数据写入Program Files下的任何文件夹(即使您创建了该文件夹)。 You should use the Environment.GetFolderPath(...) to figure out where to put your application specific data. 您应该使用Environment.GetFolderPath(...)来确定应用程序特定数据的放置位置。 You can pass in one of many enums defined here -- you probably want Environtment.SpecialFolder.CommonApplicationData 你可以传入这里定义的许多枚举之一 - 你可能想要Environtment.SpecialFolder.CommonApplicationData

Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);

I do not see how any of this is an actaul answer. 我不知道这是怎么回事。 I need to be able to write a report and have it saved the users documents folder the same folder I used to read the xml files I am writing the report from. 我需要能够编写一个报告并将它保存在用户文档文件夹中我用来读取我正在编写报告的xml文件的文件夹。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我无法在Windows 8的C#中创建XAML / DirectX应用程序? - I can't create a XAML/DirectX app in C# for Windows 8? 无法在某些 Windows 7 计算机上打开我的 c# 应用程序 - Can´t open my c# app on some Windows 7 computers 为什么我不能在C#数据库中创建表? - Why I can't create a table in my database in C#? 为什么我不能在我的代码asp.net c#中使用app_code中的代码文件 - why I can't use code files from app_code in my code asp.net c# 为什么有些 wav 文件可以在我的 c# directsound 应用程序中播放,而有些则不能? - Why do some wav files play in my c# directsound app but some don't? 为什么我不能在 LocalApplicationData 中创建文件? C#,VS 2022,Windows 11 - Why can't I create a file in LocalApplicationData? C# , VS 2022, Windows 11 在使用C#构建的Windows 8.1应用程序中,无法在listview中删除项目 - Can't drop items with in a listview in my windows 8.1 app built using c# 无法在其他计算机上使用Crystal Reports安装我的C#Windows应用程序 - Can't install my c# windows app with crystal reports on different computers 为什么我的C#编译器找不到System.Windows.Forms - Why can't my c# compiler find System.Windows.Forms 为什么我的 C# Windows 窗体应用程序生成此代码? - Why is my C# Windows Forms App generating this code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM