C#-在设置文件中存储结构

C#-在设置文件中存储结构,c#,object,struct,setting,C#,Object,Struct,Setting,我只想在设置文件中存储一些主要配置,因为配置可能随时更改。 我的应用程序的用户将能够在多个环境中管理一些东西。 每个环境都有自己的配置(基本上是到网络位置的路径) 我为每个环境构建了一个结构,但是现在我们必须添加更多的环境,所以在源代码之外存储配置是很有帮助的 那么,让我给你一些代码。我构建了两个结构来描述每个环境: public struct env_conf { string title; string path_to_xml; string path_to_sharepo

我只想在设置文件中存储一些主要配置,因为配置可能随时更改。 我的应用程序的用户将能够在多个环境中管理一些东西。 每个环境都有自己的配置(基本上是到网络位置的路径)

我为每个环境构建了一个结构,但是现在我们必须添加更多的环境,所以在源代码之外存储配置是很有帮助的

那么,让我给你一些代码。我构建了两个结构来描述每个环境:

public struct env_conf
{
   string title;
   string path_to_xml;
   string path_to_sharepoint;
   List<subfolder> subfolders;
   //Some more strings

   public env_conf(string title, string path_to_xml, string path_to_sharepoint ...)
   {
     //Constructor which is setting the variables
   }
}

public struct subfolder
{
   string folder;
   bool is_standard;

   public env_conf(string folder, bool is_standard)
   {
     //Constructor which is setting the variables
   }
}
public struct env_conf
{
字符串标题;
字符串路径_到_xml;
字符串路径\u到\u sharepoint;
列出子文件夹;
//再来几根绳子
公共环境配置(字符串标题、字符串路径到xml、字符串路径到sharepoint…)
{
//正在设置变量的构造函数
}
}
公共结构子文件夹
{
字符串文件夹;
bool是标准;
公共环境配置(字符串文件夹,bool为标准)
{
//正在设置变量的构造函数
}
}
以下是环境配置的设置方式:

var finance_conf = new env_conf("MyTitle","MyXMLPath","MySPPath",
new List<subfolder>{new subfolder("MySubFolder",true);new subfolder("MySubFolder2",false)}
);

var sales_conf = new env_conf("MySalesTitle","MySalesXMLPath","MySalesSPPath",
new List<subfolder>{new subfolder("MySalesSubFolder",true);new subfolder("MySalesSubFolder2",false)}
);
var finance_conf=new env_conf(“MyTitle”、“MyXMLPath”、“myppath”,
新列表{new subfolder(“MySubFolder”,true);new subfolder(“MySubFolder2”,false)}
);
var sales_conf=new env_conf(“MySalesTitle”、“MySalesXMLPath”、“MySalespPath”,
新建列表{新建子文件夹(“MySalesSubFolder”,true);新建子文件夹(“MySalesSubFolder2”,false)}
);
最后一步-配置实例的定义现在应该在设置文件中

到目前为止,在设置文件中保存
string
string[]
对我来说没有问题。但现在我有了更多

除此之外,我没有VisualStudio。我和SharpDevelop一起工作,到目前为止效果很好

将我的结构标记为可序列化是没有帮助的。另外,当我手动将设置的类型设置为
MyNamespace.env_conf
时,它不会出现在设置设计器中-只有一个“?”出现在类型字段中。 所以现在,我不知道如何继续。而且,我在互联网上找到的所有信息似乎对我没有帮助。请帮帮我

如何编辑我的XML设置文件? 如何编辑我的源代码


你好

我会通过创建可序列化的类来实现,然后您可以将配置文件反序列化为类的实例,然后您将拥有所有设置

例如,这些类可能看起来像:

[Serializable]
public class EnvironmentConfig
{
    public string Title { get; set; }
    public string XmlPath { get; set; }
    public string SharepointPath { get; set; }
    public List<SubFolder> SubFolders { get; set; }
    public override string ToString()
    {
        return $"{Title}: {XmlPath}, {SharepointPath}, {string.Join(", ", SubFolders.Select(s => s.Folder))}";
    }
}

[Serializable]
public class SubFolder
{
    public string Folder { get; set; }
    public bool IsStandard { get; set; }
}
[可序列化]
公共类环境配置
{
公共字符串标题{get;set;}
公共字符串XmlPath{get;set;}
公共字符串SharepointPath{get;set;}
公共列表子文件夹{get;set;}
公共重写字符串ToString()
{
返回$“{Title}:{XmlPath},{SharepointPath},{string.Join(“,”,SubFolders.Select(s=>s.Folder))}”;
}
}
[可序列化]
公共类子文件夹
{
公用字符串文件夹{get;set;}
公共布尔值是标准{get;set;}
}
然后在代码中,您可以创建类的实例,给它一些值,并将其序列化为配置文件。稍后,您可以反序列化此文件以加载用户所做的任何更改

本例创建一个默认配置并向控制台显示值。然后,用户有机会修改文件,并显示新值

// Create a default config
var defaultEnvCfg = new EnvironmentConfig
{
    Title = "USWE Environment",
    XmlPath = @"\\server\share\xmlfiles",
    SharepointPath = @"\\server\sites\enterpriseportal\documents",

    SubFolders = new List<SubFolder>
    {
        new SubFolder { Folder = "Folder1", IsStandard = true },
        new SubFolder { Folder = "Folder2", IsStandard = false }
    }
};

// Display original values:
Console.WriteLine(defaultEnvCfg.ToString());

// Serialize the config to a file
var pathToEnvCfg = @"c:\public\temp\Environment.config";
var serializer = new XmlSerializer(defaultEnvCfg.GetType());

using (var writer = new XmlTextWriter(
    pathToEnvCfg, Encoding.UTF8) { Formatting = Formatting.Indented })
{
    serializer.Serialize(writer, defaultEnvCfg);
}

// Prompt user to change the file
Console.Write($"Please modify the file then press [Enter] when done: {pathToEnvCfg}");
Console.ReadLine();

// Deserialize the modified file and update our object with the new settings
using (var reader = XmlReader.Create(pathToEnvCfg))
{
    defaultEnvCfg = (EnvironmentConfig)serializer.Deserialize(reader);
}

// Display new values:
Console.WriteLine(defaultEnvCfg.ToString());

Console.Write("\nDone!\nPress any key to exit...");
Console.ReadKey();
//创建默认配置
var defaultEnvCfg=新环境配置
{
Title=“USWE环境”,
XmlPath=@“\\server\share\xmlfiles”,
SharepointPath=@“\\server\sites\enterpriseportal\documents”,
子文件夹=新列表
{
新的子文件夹{Folder=“Folder1”,IsStandard=true},
新的子文件夹{Folder=“Folder2”,IsStandard=false}
}
};
//显示原始值:
WriteLine(defaultEnvCfg.ToString());
//将配置序列化为文件
var pathToEnvCfg=@“c:\public\temp\Environment.config”;
var serializer=新的XmlSerializer(defaultEnvCfg.GetType());
使用(var writer=newXMLTextWriter)(
pathToEnvCfg,Encoding.UTF8){格式化=格式化.Indented})
{
serializer.Serialize(writer,defaultEnvCfg);
}
//提示用户更改文件
Write($”请修改文件,完成后按[Enter]:{pathToEnvCfg});
Console.ReadLine();
//反序列化修改后的文件并用新设置更新对象
使用(var reader=XmlReader.Create(pathToEnvCfg))
{
defaultEnvCfg=(EnvironmentConfig)序列化程序。反序列化(读取器);
}
//显示新值:
WriteLine(defaultEnvCfg.ToString());
控制台。写入(“\n完成!\n按任意键退出…”);
Console.ReadKey();

为什么要使用结构而不是类?序列化有什么问题?也可能是类,你是对的。因为我只有变量,没有函数,所以结构也会这样做。我不知道为什么序列化不起作用,这就是我问的原因。我也不知道为什么序列化不起作用-因为除了设置属性之外,你没有描述你做了什么。