Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/267.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在C中保存单个用户设置#_C#_Settings - Fatal编程技术网

C# 在C中保存单个用户设置#

C# 在C中保存单个用户设置#,c#,settings,C#,Settings,我正致力于在.NET C#应用程序中实现用户设置的保存,有一次我只想保存一个设置。是否可以这样做,或者是我使用标准一次保存所有用户设置的唯一选项: Properties.Settings.Default.Save(); 我更喜欢使用XML配置文件,并将其存储在Environment.SpecialFolder.ApplicationData等位置 可能没有.NET设置那么简单。我从来没有真正使用过它们 例如,我有下面的课程。我所需要做的就是获取或设置属性,并自动加载/保存这些属性: using

我正致力于在.NET C#应用程序中实现用户设置的保存,有一次我只想保存一个设置。是否可以这样做,或者是我使用标准一次保存所有用户设置的唯一选项:

Properties.Settings.Default.Save();
我更喜欢使用XML配置文件,并将其存储在
Environment.SpecialFolder.ApplicationData
等位置

可能没有.NET设置那么简单。我从来没有真正使用过它们

例如,我有下面的课程。我所需要做的就是获取或设置属性,并自动加载/保存这些属性:

using Nini.Config;

public class DbConfig : PropertyNotifierBase {
    private static readonly string PROGRAM_NAME = "programname";
    private static readonly string CONFIG_NAME = "Database";

    private static DbConfig _instance = new DbConfig();

    public static DbConfig Instance { get { return (_instance); } }

    private DbConfig() {
        SetupPaths();
        Source = new XmlConfigSource(FullConfigFilename);
        Source.AutoSave = true;
        CreateSectionsIfNeeded();
    }

    private void CreateSectionsIfNeeded() {
        if (Source.Configs["Database"] == null)
            Source.AddConfig("Database");
    }

    private void SetupPaths() {
        ConfigPath = DetermineConfigPath();
        ConfigFilename = String.Format("{0}.xml", CONFIG_NAME);
        Directory.CreateDirectory(ConfigPath);

        // Create an empty configuration file if it isn't there.
        if (!File.Exists(FullConfigFilename))
            File.WriteAllText(FullConfigFilename, "<Nini>\n</Nini>\n");
    }

    private IConfigSource Source { get; set; }

    public String ConfigPath { get; private set; }

    public String ConfigFilename { get; private set; }

    public String FullConfigFilename { get { return (Path.Combine(ConfigPath, ConfigFilename)); } }

    public String SqlServerInstance {
        get { return (Source.Configs["Database"].GetString("SqlServerInstance", @"somedefaultconnection")); }
        set { Source.Configs["Database"].Set("SqlServerInstance", value); NotifyPropertyChanged("SqlServerInstance"); }
    }

    public String SqlServerDatabase {
        get { return (Source.Configs["Database"].GetString("SqlServerDatabase", "somedatabasename")); }
        set { Source.Configs["Database"].Set("SqlServerDatabase", value); NotifyPropertyChanged("SqlServerDatabase"); }
    }

    public String SqlServerUsername {
        get { return (Source.Configs["Database"].GetString("SqlServerUsername", "someusername")); }
        set { Source.Configs["Database"].Set("SqlServerUsername", value); NotifyPropertyChanged("SqlServerUsername"); }
    }

    public String SqlServerPassword {
        get { return (Source.Configs["Database"].GetString("SqlServerPassword", "somepassword")); }
        set { Source.Configs["Database"].Set("SqlServerPassword", value); NotifyPropertyChanged("SqlServerPassword"); }
    }

    private string DetermineConfigPath() {
        String filename = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
        filename += Path.DirectorySeparatorChar + PROGRAM_NAME;
        return (filename);
    }
}
使用Nini.Config;
公共类DbConfig:PropertyNotifierBase{
私有静态只读字符串程序\u NAME=“programname”;
私有静态只读字符串配置_NAME=“数据库”;
私有静态DbConfig_instance=new DbConfig();
公共静态DbConfig实例{get{return(_Instance);}
私有数据库配置(){
设置路径();
Source=新的XmlConfigSource(FullConfigFilename);
Source.AutoSave=true;
createSectionSifRequired();
}
私有void createSectionSifRequired(){
if(Source.Configs[“数据库”]==null)
Source.AddConfig(“数据库”);
}
专用void setuppath(){
ConfigPath=DetermineConfigPath();
ConfigFilename=String.Format(“{0}.xml”,配置名称);
创建目录(ConfigPath);
//如果不存在,请创建一个空配置文件。
如果(!File.Exists(FullConfigFilename))
File.WriteAllText(FullConfigFilename,“\n\n”);
}
专用IConfigSource源{get;set;}
公共字符串配置路径{get;private set;}
公共字符串ConfigFilename{get;private set;}
公共字符串FullConfigFilename{get{return(Path.Combine(ConfigPath,ConfigFilename));}
公共字符串SqlServerInstance{
获取{return(Source.Configs[“Database”].GetString(“SqlServerInstance”,“somedefaultconnection”);}
set{Source.Configs[“Database”].set(“SqlServerInstance”,value);NotifyPropertyChanged(“SqlServerInstance”);}
}
公共字符串SqlServerDatabase{
获取{return(Source.Configs[“Database”].GetString(“SqlServerDatabase”,“somedatabasename”);}
set{Source.Configs[“Database”].set(“SqlServerDatabase”,value);NotifyPropertyChanged(“SqlServerDatabase”);}
}
公共字符串SqlServerUsername{
获取{return(Source.Configs[“Database”].GetString(“SqlServerUsername”,“someusername”);}
set{Source.Configs[“Database”].set(“SqlServerUsername”,value);NotifyPropertyChanged(“SqlServerUsername”);}
}
公共字符串SqlServerPassword{
获取{return(Source.Configs[“Database”].GetString(“SqlServerPassword”,“somepassword”);}
set{Source.Configs[“Database”].set(“SqlServerPassword”,value);NotifyPropertyChanged(“SqlServerPassword”);}
}
私有字符串DetermineConfigPath(){
字符串文件名=Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
filename+=Path.directoryseportorchar+程序名;
返回(文件名);
}
}

您可以让用户编辑的对象与
Properties.Settings.Default
不同,并且在调用
Save()
Steve之前,只能将您要保存的设置从对象传输到该对象中,您能举个例子说明如何执行此操作吗?我是一个初学者。我刚刚编辑并发布了一个我使用的类。这很容易。包括对NINI DLL的引用,以及上面的一小段代码,这些都是需要的。顺便说一下,在这个类中,我保存了数据库连接的连接信息,密码保存在明文中。不是很安全,但对于我的应用程序来说,我并不担心。