当前位置:编程学习 > C#/ASP.NET >>

C#怎么创造一个配置文件?

然后怎么进行读/写? C# --------------------编程问答-------------------- 看这个配置文件是什么格式的,ini还是config或者是xml,这些百度一下都能够找到答案。 --------------------编程问答-------------------- 添加新项 选应用程序配置文件 就会多出一个app.config文件。
可以使用 system.configuration.configurationmanager类读取配置文件。 --------------------编程问答--------------------
引用 2 楼 yaotomo 的回复:
添加新项 选应用程序配置文件 就会多出一个app.config文件。
可以使用 system.configuration.configurationmanager类读取配置文件。


貌似他问的不是这样... --------------------编程问答--------------------
引用 2 楼 yaotomo 的回复:
添加新项 选应用程序配置文件 就会多出一个app.config文件。
可以使用 system.configuration.configurationmanager类读取配置文件。


这是最简单的办法了。

事实上配置文件也是一个文本文件,甚至是一个xml文件,那么直接使用那些文本文件读写和xml读写的api也是可以的。 --------------------编程问答-------------------- http://www.google.ee/search?q=site%3Acnblogs.com%20C%23%E6%93%8D%E4%BD%9C%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6 这里有很多实现 --------------------编程问答-------------------- 嗯嗯。我一般使用GetPrivateProfileString方法和WritePrivateProfileString方法对txt或config文件进行读取和写入,XML文件就不用说了吧。

具体可以参考
http://blog.sina.com.cn/s/blog_4369de7e0100guz8.html --------------------编程问答-------------------- 都是热心人啊 --------------------编程问答-------------------- 1.遍历要保存的控件名和值
2.序列化保存Xml
3.读取的时候反序列化,根据控件类型赋值。


#region 得到界面的键值队
        /// <summary>
        /// 得到界面的键值队
        /// </summary>
        /// <param name="controls"></param>
        private bool searchControl(System.Windows.Forms.Control.ControlCollection controls)
        {
            foreach (Control c in controls)
            {
                if (c is System.Windows.Forms.GroupBox || c is System.Windows.Forms.TabControl || c is System.Windows.Forms.TabPage)
                {
                    searchControl(c.Controls);
                }
                else
                {
                    if (c is System.Windows.Forms.NumericUpDown)
                    {
                        configdic.Add(c.Name, ((NumericUpDown)c).Value.ToString());
                    }
                    if (c is System.Windows.Forms.DateTimePicker)
                    {
                        //configdic.Add(c.Name, ((DateTimePicker)c).Value.ToString());
                        configdic.Add(c.Name, ((DateTimePicker)c).Text);
                    }
                    if (c is System.Windows.Forms.TextBox)
                    {
                        configdic.Add(c.Name, c.Text.Trim());
                    }
                    if (c is System.Windows.Forms.CheckBox)
                    {
                        if (((CheckBox)c).Checked)
                        {
                            configdic.Add(c.Name, "1");
                        }
                        else
                        {
                            configdic.Add(c.Name, "0");
                        }
                    }
                    if (c is System.Windows.Forms.RadioButton)
                    {
                        if (((RadioButton)c).Checked)
                        {
                            configdic.Add(c.Name.Substring(0, c.Name.Length - 1), c.Name.Substring(c.Name.Length - 1, 1));
                        }
                    }
                }
            }
            return true;
        }
        #endregion



#region 获取界面控件的名称 或则赋值
        /// <summary>
        /// 获取全部控件名称
        /// </summary>
        /// <param name="controls"></param>
        /// <param name="type">0获取名称,1显示值</param>
        private void searchControlname(System.Windows.Forms.Control.ControlCollection controls, int type)
        {
            foreach (Control c in controls)
            {
                if (c is System.Windows.Forms.GroupBox || c is System.Windows.Forms.TabControl || c is System.Windows.Forms.TabPage)
                {
                    //如果是空间组则进入
                    searchControlname(c.Controls, type);
                }
                else
                {

                    //如果是数值框
                    if (c is System.Windows.Forms.NumericUpDown)
                    {
                        if (type == 0)
                        {
                            nodes.Add(c.Name);
                        }
                        else
                        {
                            if (configdic.ContainsKey(c.Name))
                            {
                                ((NumericUpDown)c).Value = Convert.ToInt32(configdic[c.Name]);
                            }
                        }
                    }
                    //如果是文本框
                    if (c is System.Windows.Forms.TextBox)
                    {
                        if (type == 0)
                        {
                            nodes.Add(c.Name);
                        }
                        else
                        {
                            if (configdic.ContainsKey(c.Name))
                            {
                                c.Text = configdic[c.Name];
                            }
                        }
                    }
                    //如果是复选框
                    if (c is System.Windows.Forms.CheckBox)
                    {
                        if (type == 0)
                        {
                            nodes.Add(c.Name);
                        }
                        else
                        {
                            if (configdic.ContainsKey(c.Name))
                            {
                                if (configdic[c.Name] == "1")
                                {
                                    ((CheckBox)c).Checked = true;
                                }
                            }
                        }
                    }
                    //如果是单选按钮
                    if (c is System.Windows.Forms.RadioButton)
                    {
                        if (type == 0)
                        {
                            if (!nodes.Contains(c.Name.Substring(0, c.Name.Length - 1)))
                            {
                                nodes.Add(c.Name.Substring(0, c.Name.Length - 1));
                            }
                        }
                        else
                        {
                            if (configdic.ContainsKey(c.Name.Substring(0, c.Name.Length - 1)))
                            {
                                if (c.Name == c.Name.Substring(0, c.Name.Length - 1) + configdic[c.Name.Substring(0, c.Name.Length - 1)])
                                {
                                    ((RadioButton)c).Checked = true;
                                }
                            }
                        }
                    }
                    //如果是时间
                    if (c is System.Windows.Forms.DateTimePicker)
                    {
                        if (type == 0)
                        {
                            nodes.Add(c.Name);
                        }
                        else
                        {
                            if (configdic.ContainsKey(c.Name))
                            {
                                ((DateTimePicker)c).Value = Convert.ToDateTime(configdic[c.Name]);
                            }
                        }
                    }
                }
            }
        }
        #endregion
--------------------编程问答-------------------- Xml操作自己弄吧,我不这么做那块。 --------------------编程问答--------------------
引用 楼主 u010147020 的回复:
然后怎么进行读/写?


参考:http://itworktor.blog.163.com/blog/static/17520302920105102952780/ --------------------编程问答--------------------
为什么加不了OAO
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,