如何在应用程序运行之间存储单个文件名 [英] How do I store a single filename between application runs

查看:40
本文介绍了如何在应用程序运行之间存储单个文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我只想存储文件名的单个条目,我可以在appication属性的用户设置部分执行此操作。或者我应该只将这些设置用于Windows位置和用户首选项...



我想要一个简单的程序,它将显示一个向导来获取第一个文件名,然后加载此文件并提供在菜单中更新的选项。



亲切的问候



Andrew

If I only want to store a single entry of a filename, could I do this in the user settings part of the appication properties. Or should I only use these settings for windows location and user preference things...

I want a simple program that will show a wizard to get the first filename and subsequently load with this file and provide an option to update in a menu.

Kind regards

Andrew

推荐答案

用户范围设置 [ ^ ]就是为了这个。但当然你也可以使用注册表(HKCU)来达到同样的目的。
User scope setting[^] are just for that. But of course you can also use registry (HKCU) for the same purpose.


首先,请看我对解决方案2的评论。



主要思想是:您应该将中间数据和配置数据保存在特殊文件夹中,其中一个是为所有用户按用户设置的。请参阅我过去的回答:

如何找到我的程序目录 [ ^ ]。



原则上,设置文件是使用此方法的方法之一,因此您可以使用建议解决方案2.



但是,我认真考虑设置在道德上过时,从不使用它们。我使用了一种不同的方法,该方法在引入设置(使用WCF)后出现,并提供更好的可维护性。这只是基于数据合同序列化 https://msdn.microsoft.com/en-us/library/ms733127%28v=vs.110%29.aspx [ ^ ]。



这种方法与WCF无关,仅供WCF使用,对各种设置来说都是最方便的,因为所有设置的自动化都要好得多。从本质上讲,您创建任何对象图(使其成为合同只需添加一些.NET属性)的任意数据,没有任何限制(不需要使用任何特定的基类或实现任何接口,访问控制关键字也无所谓并告诉系统将其全部存储在任何文件或流中。稍后,这些数据将以之前的方式恢复到内存。您的数据甚至不是树,它可以是具有任意关系的对象图。当然,它可能只是一种数据类型(类,结构),您的文件名可以是属性或字段。这就是全部。



-SA
First of all, please see my comment to Solution 2.

Main idea is: you should keep your intermediate and configuration data in a "special folder", one of those set up per user of for "all users". Please see my past answer:
How to find my programs directory[^].

In principle, the settings file is one of the way to use this approach, so you can use the advice of Solution 2.

However, I seriously consider setting as morally obsolete and never use them. I use a bit different approach which appeared after the setting was introduced (with WCF) and provides much better maintainability. This is just serialization based on Data Contract: https://msdn.microsoft.com/en-us/library/ms733127%28v=vs.110%29.aspx[^].

This approach pe se has nothing to do with WCF, is only used by WCF and is the most convenient for all kinds of settings, because everything is automated much better as with settings. Essentially, you create any object graph (making it a contract only takes adding some .NET attributes) of any arbitrary data, without any limitations (no need to use any certain base class or implement any interfaces, access control keywords also don't matter) and just tell the system to store it all in any file or a stream. Later on, this data will be restored to memory the way it was before. Your data does not even be a tree, it could be a graph of objects with any arbitrary relationships. Of course, it could be just one data type (class, structure), and your file name can be a property or a field. That's all.

—SA


>>我可以在应用程序属性的用户设置部分执行此操作(原文如此)



我不明白为什么不 - 我相信人们会有各种各样的'宗教''你必须/不能/不应该规则',但是对于低音量(每个应用程序运行1次更新)有点我不知道为什么你会让它变得比它必须更难



(如果你说你需要每秒更新'储值',那么app.config不是一个好选择)



'g'
>> could I do this in the user settings part of the appication properties (sic)

I don't see why not - I'm sure people will have all sorts of 'religious' "you must/cant/shouldn't rules", but for a low volume (1 update per app run) sort of thing I cant see why you'd make it harder than it has to be

(if you said you needed to update a 'stored value' every second, then the app.config isn't a good choice)

'g'


这篇关于如何在应用程序运行之间存储单个文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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