简体   繁体   English

类库中的.net负载配置

[英].net load configuration in class library

let's say i have : 1) 1 WindowsForm on "A" Project 2) 1 WindowsForm on "B" Project 3) 1 class library (GAC) 假设我有:1)1个“ A”项目上的WindowsForm 2)1个“ B”项目上的WindowsForm 3)1个类库(GAC)

Condition Both of Project references is same 条件两个项目引用均相同

Part 1 : I have my.settings in my class library to save configuration with public function 第1部分:我的类库中有my.settings以保存具有公共功能的配置

Part 2 : I create value/configuration from "A" and store it in my class library. 第2部分:我从“ A”创建值/配置并将其存储在类库中。 settings has been successfully saved, and load the value/configuration with no errors 设置已成功保存,并正确加载值/配置

Question : 1). 问题: 1)。 Why i can't load the value/configuration from "B" ? 为什么我不能从“ B”加载值/配置? NullException shown First I think, to use my.resources in class library but, my.resources is readonly 首先显示我想在类库中使用my.resources,但my.resources是只读的

2). 2)。 What best solution to connecting 1 class library to multiple project 将1个类库连接到多个项目的最佳解决方案

code in class library to save value 类库中的代码以节省价值

Public Sub Kucing_simpan(ByVal value As String)
    My.Settings.Kucing = value
    My.Settings.Save()
End Sub

code in class library to load value 类库中的代码以加载值

Public Function kucing_ambil()
    Dim value As String
    value = My.Settings.Kucing
    Return value
End Function

code in "A" “ A”中的代码

dim save as new Zombie.Kencing 'My class Library Name
save.Kucing_simpan(textbox1.text)

code in "B" “ B”中的代码

dim load as new Zombie.Kencing 'My class Library Name
DataGridView1.Rows(0).Cells(1).Value = load.kucing_ambil

You cant do what you are trying to do - at least not the way you are going about it. 您无法做您想做的事情-至少不能做到。

First, you have to understand how/where Settings are saved. 首先,您必须了解如何/在何处保存设置。 With default naming of the app and such, project A - WindowsApplication1 will save its settings to something cryptic such as: 使用应用程序等的默认命名,项目A- WindowsApplication1会将其设置保存到一些神秘的东西,例如:

C:\Users\<UserName>\AppData\Local\Microsoft\_
    WindowsApplication1_Url_ggn13vigzyicmtfkwaa3vi5tyxn0sy3r\1.0.0.0\user.config

NET creates the hash to make sure that apps with the same name have a different safe location to store settings. NET创建哈希以确保具有相同名称的应用程序具有不同的安全位置来存储设置。 So, WindowsApplication3 will have a different hash; 因此, WindowsApplication3将具有不同的哈希值。 this is also how your 17th project with the name WindowsApplication1 doesnt accidentally load or find the settings of WinApp 1-16. 这也是您名为WindowsApplication1第17个项目不会意外加载或找到WinApp 1-16设置的方式。

Next, your Settings Class Lib is not a separate application. 接下来,您的设置类库不是单独的应用程序。 As a DLL, it is operating as if it was a set of functions and such associated with the App calling it. 作为DLL,它的运行就像是一组函数,并且与调用它的App相关联。 So when Project A saves settings thru the ClassLib, they are saved to a different location than Project B. Even using a Class Lib, NET uses Application credentials and info to concoct the filename and path. 因此,当Project A通过ClassLib保存设置时,它们将保存到与Project B不同的位置。即使使用Class Lib,NET也使用应用程序凭据和信息来编写文件名和路径。


What you can do is write a Class which defines all the possible settings (or creates a List or Dictionary of them) then save to a fixed, known location such as: 可以做的是编写一个类,定义所有可能的设置(或创建它们的ListDictionary ),然后保存到固定的已知位置,例如:

Private Comp As String = "ZiggySoft"
Private Prod As String = "ZiggyWare"

Private settingsFile As String

'...
settingsfile = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
settingsfile = Path.Combine(settingsfile, Comp, Prod, "usersettings.dat")

This will result in: "C:\\Users\\\\AppData\\Roaming\\ZiggySoft\\ZiggyWare\\usersettings.dat" 这将导致:“ C:\\ Users \\\\ AppData \\ Roaming \\ ZiggySoft \\ ZiggyWare \\ usersettings.dat”

Include the same file in each project (Add Existing Item, maybe pick Add As Link from the dropdown). 在每个项目中包括相同的文件(添加现有项,或者从下拉列表中选择“添加为链接”)。 Now, you can read and write your Settings to a file you are in charge of. 现在,您可以将“设置”读写到您负责的文件中。 You can save/load the entire class or List in 3-5 lines of code if you serialize it. 如果序列化整个类或列表,则可以在3-5行代码中保存/加载整个类。

When Project A loads the settings, it would also get those which only apply to B or J or V, but common ones would and could be shared. 当项目A加载设置时,它还将获得仅适用于B或J或V的那些设置,但可以共享并且可以共享这些通用设置。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM