我应该在WPF中将我的应用程序特定的配置文件存储在哪里

Where should I store my App specific config files in WPF
2021-07-22
  •  译文(汉语)
  •  原文(英语)

背景:我有一些应用程序数据.即数据库,重要的配置文件.此数据对于应用程序启动至关重要,否则将被退出.

问题:我应该在哪里存储这些数据.即在哪个文件夹和哪里.现在(这是错误的),它存储在Debug / App_Data中的文件夹中.但是在git中引起问题,当我们发布该应用程序时,找不到数据.那么我们可以在哪里存储此文件夹?

当前结构为"WpfApplication2 \ WpfApplication2 \ bin \ Debug"

启动应用程序时,这些文件需要存在.因此,它们需要成为应用程序本身的一部分.

速聊1:
将这些文件包括到您的项目中,并将"复制到输出目录"属性设置为"始终复制"
速聊2:
我会尝试的.请将此添加为答案
速聊3:
没必要.首先尝试:)
速聊4:
您的答案是正确的.请将此添加为答案,我将其标记为正确.
解决过程1

将这些文件包括到您的项目中,并将属性设置Copy to Output directoryCopy always

解决过程2

建议使用:

var pathToConfig = Path.Combine( System.Environment.GetFolderPath(
           Environment.SpecialFolder.CommonApplicationData), YOUR_APP_NAME);

在Windows Vista和+机器上,它将以"C:\ ProgramData \ AllUser \ YOUR_APP_NAME"结尾

在Windows XP上,"C:\ Document and Settings \ Application Data \ YOUR_APP_NAME"

在这两种情况下,您都将由OS自己保证在这些位置具有写许可权,并且它们在用户眼中"隐藏"(默认情况下,这两个文件夹都是隐藏的)

正如@Athari正确建议的那样:YOUR_APP_NAME应该是YOUR_COMPAMY_NAME \ YOUR_APP_NAME

速聊1:
推荐的方法是使用"CompanyName \ AppName",而不仅仅是"AppName".文件夹是AFAIK的"C:\ ProgramData"和"C:\ Document and Settings \ All Users \ Application Data".
解决过程3

您可以将应用程序特定的文件保存在

Path.Combine(Environment.GetFolderPath(
    Environment.SpecialFolder.LocalApplicationData),
    Path.GetFileNameWithoutExtension(AppDomain.CurrentDomain.FriendlyName));

但您也可以使用其他文件夹My Documents.

编辑:我记得我曾经在.NET 3.0中的WPF应用程序中使用过隔离存储(我认为):

隔离存储简介

执行隔离存储任务

速聊1:
使用"文档"文件夹是一个坏主意.由于各种应用程序不断向该文件夹发送垃圾邮件,因此该文件夹几乎变得无法使用.使用程序集名称也是一个坏主意,因为推荐的方法是使用"CompanyName \ AppName".使用程序集名称将导致该文件夹中包含毫无意义的文件夹.最后,最初的问题表明数据是特定于安装的,而不是特定于用户的.
速聊2:
这些要点也适用于系统上的任何其他文件夹.在"CompanyName \ AppName"上;我同意.我自己做;这个答案只是为了解释这个想法.而且-在某些情况下-My Documents是一个合乎逻辑的地方;特别是当您打算重用和共享生成的数据时-从用户的角度来看.对于特定于应用程序的数据,可以Environment.SpecialFolder.CommonApplicationData代替使用.顺便说一句,您有什么建议?
解决过程4

您的App.config应该放在.NET桌面应用程序的正常位置:它应该是yourApplication .exe.config与.exe所在的文件夹.

Background : I have some Application Data. i.e. the Database, come important config files. This data is vital for the application to start else it is exited.

Problem : Where should I store this data. i.e in which folder and where. Right Now (This is wrong) it is stored in a folder in Debug/App_Data. But is causing issues in git and when we publish the App the data is not found. So where can we store this folder ?

Present Structure is "WpfApplication2\WpfApplication2\bin\Debug"

These Files need to be present when the app is started. So they need to be a part of the app itself.

Talk1:
Include those files to your project and set attribute "Copy to Output directory" to "Copy always"
Talk2:
I will try this. Please add this as an answer
Talk3:
Not neccessary. Try it first :)
Talk4:
yours was the correct answer. Please add this as an answer I will mark it correct.
Solutions1

Include those files to your project and set attribute Copy to Output directory to Copy always

Solutions2

Would suggest use:

var pathToConfig = Path.Combine( System.Environment.GetFolderPath(
           Environment.SpecialFolder.CommonApplicationData), YOUR_APP_NAME);

On Windows Vista and + machine this will end in "C:\ProgramData\AllUser\YOUR_APP_NAME"

On Windows XP "C:\Document and Settings\Application Data\YOUR_APP_NAME"

in both cases, you guaranteed by OS itself to have write permission in these location, and its "hidden" from the users eyes (both folders are hidden by default)

As @Athari correctly suggests: YOUR_APP_NAME should be YOUR_COMPAMY_NAME\YOUR_APP_NAME

Talk1:
The recommended way is to use "CompanyName\AppName", not just "AppName". And the folders are "C:\ProgramData" and "C:\Document and Settings\All Users\Application Data", AFAIK.
Solutions3

You can save your application specific files at

Path.Combine(Environment.GetFolderPath(
    Environment.SpecialFolder.LocalApplicationData),
    Path.GetFileNameWithoutExtension(AppDomain.CurrentDomain.FriendlyName));

but you can use other folders like My Documents too.

Edit: I've remembered I have used Isolated Storage in a WPF app back in .NET 3.0 (I think):

Introduction to Isolated Storage

Performing Isolated Storage Tasks

Talk1:
Using "Documents" folder is a BAD idea. Because of constant spamming of the folder from all kinds of applications, it became pretty much unusable. Using assembly name is a bad idea too, as the recommended way is to use "CompanyName\AppName". Using assembly name will result in spamming the folder with nonsensical folders. And finally, the original question suggests that data is installation-specific, not user-specific.
Talk2:
Those points applies to any other folder on the system, too. On "CompanyName\AppName"; I agree. I do it myself; and this answer was just to explain the idea. And - for some scenarios - My Documents is a logical place; especially when you intend to reuse and share the generated data - from the user point of view. And for app specific data one could use Environment.SpecialFolder.CommonApplicationData instead. BTW what is your suggestion?
Solutions4

Your App.config should be in the normal place for .NET desktop applications: it should be yourApplication.exe.config in the same folder as the .exe.

转载于:https://stackoverflow.com/questions/17362225/where-should-i-store-my-app-specific-config-files-in-wpf

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入