定义所在的user.config文件应保存自定义路径?自定义、路径、所在、定义

2023-09-03 03:22:27 作者:10.眉眼朦胧

如果我重新命名我的,例如从程序myapp.exe 编译应用程序 APP.EXE 然后当我运行改名可执行一个新的用户设置文件夹,在这个路径中产生的:

If I rename my compiled application for example from myapp.exe to app.exe then when I run the renamed executable a new user settings folder is generated in this path:

C:\Users\{User}\AppData\Local\{CompanyName}\{ExecutableName}_Url_{SystemGUID or something strange}

所以,我失去所有保存的设置。

So I loose all the saved settings.

那么我怎么能解决这个问题,定义在 VBNET 的WinForms 我自己的位置来存储 user.config 文件,或使用的applicationSettings基础设施的任何其他的解决办法? (不保存在注册表或其他东西的设置)

Then how I could solve this problem defining in a VBNET WinForms my own location to store the user.config file, or any other solution using the applicationsettings infrastructure ? (not saving the settings on the registry or other things)

PS:我读过这太帖子里面有一点点不同的问题,但反正我不明白所谓的解决方案Can我控制.NET用户设置的位置,以避免失去对应用程序的设置升级?

PS: I've read this SO post which is a little bit different question but anyways I didn't understood the supposed solution Can I control the location of .NET user settings to avoid losing settings on application upgrade?

推荐答案

更​​多信息,并从链接珍闻,回答你的问题:

More info and a tidbit from the link which answers your question:

在systemGUID什么你引用其实是两件事情(参考的 MSDN My.Settings ):

The "systemGUID or something" you reference is actually a hash of 2 things (Reference MSDN My.Settings):

<eid> is the URL, StrongName, or Path, based on the evidence available to hash.  
<hash> is a SHA1 hash of evidence gathered from the CurrentDomain, 
    in the following order of preference: 
    - StrongName 
    - URL If neither of these is available, use the .exe path.

如果没有强名称,您的位置改变由路径是您所描述的问题。因为这两个EID和散列将使用强名称散列(ES),即使它们移到别的地方的完整路径应保持不变或安装新版本。当使用强名称的凭证来从应用程序和散列不改变,并且从未使用最后一招(exe文件路径)的方法。其中回答您的基本问题:使用强名称和路径不会改变。

Without a StrongName, your location is varying by path which is the problem you describe. Since BOTH eid and hash will use StrongName for the hash(es), the full path should remain the same even if they move it somewhere else or install a new version. When using a StrongName the credentials come from the app and the hashes don't change and the method of last resort (the exe path) is never used. Which answers your basic question: use a Strong Name and the path wont change.

新版本/版本将创建一个文件夹,每个版本的设置下的子文件夹树。该升级方法设置中提到的链接(显然)有利于进口从设置/ A previous版本。在EXE名称的改变将导致AppDomain.FriendlyName(第三单元)来,虽然改变。

New releases/versions will create a sub folder tree under that folder for each version for Settings. The Upgrade method for Settings mentioned in the link (apparently) facilitates importing Settings from the/a previous version. A change in the EXE Name will cause the AppDomain.FriendlyName (3rd element) to change though.

独立存储是另一种选择,它是不是很难,因为它首先查找,但也有类似的行为。符合ISO,你不指定一个文件夹,因为它只是在一个不起眼的位置,如用户\&LT创建一个;使用者&gt; \独立存储\ zhxytg \ dhfyres \ 。该位置可以保持相同的所有版本的应用程序中,即使你重新命名它,如果你使用的ClickOnce(因此,这是另一种可行的解决方案)。

Isolated Storage is another option, and it is not as hard as it first looks, but has similar behavior. With Iso, you dont specify a folder as it just creates one in an obscure location like Users\<User>\Isolated Storage\zhxytg\dhfyres\. The location CAN remain the same for all versions of the app, even if you rename it, if you use ClickOnce (so, this is another viable solution).

我认为你必须使用的ClickOnce(强名称作为替代犯规拿出MSDN中)得到应用水平的证据。作为一个附带的好处,符合ISO,即使在最高级别的安全非管理员用户可以读取/写入共享文件的 ProgramData \ AllUsers的(正如所牌照的情况下或共享设置,一个应用程序套件)至少有W7。该应用程序的哈希值允许它来写这条道路,所以它可以做一些事情,我们通常无法做到的。

I think you have to use ClickOnce (StrongName as a replacement doesnt come up in MSDN) to get Application level evidence. As a side benefit, with ISO, even under the highest security a non admin user can read/write to shared files in ProgramData\AllUsers (as might be the case for a licence, or shared settings for an app suite) at least with W7. The app's hash permits it to write to that path, so it can do some things we normally can't do.

如果你不使用的ClickOnce,你仍然可以得到每安装一个稳定的文件夹中的 的和读/写 AllUsers的。新安装(到不同的文件夹),将导致不同的散列和文件位置;同改变的文件名。即使你设法保存旧位置的地方,一个新的安装可能不会有权限的旧文件(还没有尝试过)。

If you dont use ClickOnce, you can still get a stable folder per install and read/write to AllUsers. A new install (to a different folder) will result in a different hash and file location; same with changing the filename. Even if you managed to store the old location somewhere, a new install probably would not have rights to the old file (havent tried).

ISO除去由EXENAME改变,但它不使用My.Settings。相反,你使用 IsolatedFileStreams IsolatedStorageFile 对象创建。而你不得不接管组织和管理的各种设置的值和名称。使用(应用/用户)独立存储的类型取决于可用的凭据。

ISO removes varying by EXEName, but it does not use My.Settings. Instead you use IsolatedFileStreams created by IsolatedStorageFile objects. And you'd have to take over organizing and managing the values and names of the various Settings. The type of Isolated Storage used (App / User) depends on the credentials available.

隔离存放有它的地方,但似乎是矫枉过正的设置。

Isolated storage has its place, but seems to be overkill for Settings.

您提到,您通常只使用MySettings为琐碎的应用程序。这样,一个强名称只是为了稳定用于设置似乎是矫枉过正的路径。 ISO是非常有趣的,但也有一些是非常简单的。这第三个选项落入或其他东西你不想要的,但的非常的灵活性。

You mentioned that you usually only use MySettings for trivial apps. As such, a StrongName simply to stabilize the path for Settings seems to be overkill. ISO is very interesting, but there is something much simpler. This third option falls into the or other things you didn't want, but is very flexible.

建立你自己的设置类各地的序列化。对于简单的设置,这些可能的arent不是一组的名称 - 值对{LastPath =.....多; FormLeft = X; FormTop = Y ...}。在词典保存这些(字符串,字符串)词典(enumSettings中,字符串)和公正的序列化(除)整个容器:

Build your own Settings Class around Serialization. For simple settings, these likely arent much more than a set of Name-Value Pairs {LastPath = "....."; FormLeft = x; FormTop = y ...}. Save these in a Dictionary(Of String, String) or Dictionary(Of enumSettings, String) and just serialize (save) the entire container:

Dim bf As New BinaryFormatter
Using fs As New FileStream(myFile, FileMode.OpenOrCreate)
    bf.Serialize(fs, _UserOpts)   
End Using

获取值回来也很简单。对于更复杂的项目中有很多种类,以节省喜欢整数,日期,数组,ArrayList中,列表(对T)等,创建一个USEROPTIONS类为他们和序列化的的代替。

请注意,你传递一个文件流的序列化,所以你可以完全控制的名称和位置,如 C:\用户\&lt;用户名&GT; \应用程序数据\本地\&LT;公司&GT; \&LT;产品&GT; \ Settings.bin 通过的版本,文化,集会等的位置不会改变它会留在你把它

Note that you pass a filestream to the serializers, so you have full control over the name and location, such as C:\Users\<username>\AppData\Local\<Company>\<Product>\Settings.bin The location wont change by version, culture, assembly etc. It will stay where you put it.

这不会虎头蛇尾,当您尝试serilize像点,大小和字体类型,因为对象不能直接序列化。尤其是,与ProtoBuff可以有多种选择,以这些转换的东西可序列的飞行或预先。

This does run out of steam when you try to serilize Types like Point, Size and Font because Objects cannot be serialized directly. Especially, with ProtoBuff there are multiple options to convert these to something serializable on the fly or beforehand.