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

WinForm配置文件读写问题

我现在做了两个单独的程序A,B,A有配置文件,B没有配置文件,现在将A,B放到一个文件夹下,我想用B来读写A的配置文件,不知道如何操作,声明XML读写我也不会,而且看了一点,貌似比较繁琐,
谢谢各位帮忙的朋友 --------------------编程问答-------------------- 只能自己指定要读的配置文件名了,A的配置文件会默认为A.exe.Config,B没有的话只能自己指定路径 --------------------编程问答--------------------
引用 1 楼 whslovexyp 的回复:
只能自己指定要读的配置文件名了,A的配置文件会默认为A.exe.Config,B没有的话只能自己指定路径


指定文件名后,如何读写,这个事重点啊~~ --------------------编程问答-------------------- 我现在做了两个单独的程序A,B,A有配置文件,B没有配置文件,现在将A,B放到一个文件夹下,我想用B来读写A的配置文件,不知道如何操作,声明XML读写我也不会,而且看了一点,貌似比较繁琐,
谢谢各位帮忙的朋友 --------------------编程问答-------------------- 配置文件问题吧? --------------------编程问答-------------------- 写个XML文件
哪个程序都能读
读写XML文件的程序搜索下吧
其实很简单的 --------------------编程问答-------------------- 读取app.config中自定义配置的值的属性,常用2种方法.
假设有如下配置:
<appSettings> 
<add key="A" value="config with A"/> 
<add key="B" value="config with B"/> 
</appSettings> 

using System.Configuration;
[A] 方法
string strTest = ConfigurationSettings.AppSettings["A"];    // get A 's value
[B] 方法
AppSettingsReader appReader = new AppSettingsReader();
string strTest = appReader.GetValue(strKey,typeof(string)).ToString();
--------------------编程问答--------------------

读取app.config中的数据
App.config中格式如下:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="UserInfo" type="System.Configuration.AppSettingsSection"/>
    <section name="directoryInfo" type="System.Configuration.AppSettingsSection"/>
  </configSections>
  <UserInfo>
    <add key="DVRUSR" value="" />
    <add key="DVRPWD" value="" />
  </UserInfo>  
  <directoryInfo>
    <add key="defaultPath" value="22" />
    <add key="savePath" value="11" />
  </directoryInfo>
</configuration>

读取方式如下:

string a = GetSettingValue("directoryInfo", "savePath");
///////////////////////////////////////////////////////////////////////////////
private string m_configFile = System.Reflection.Assembly.GetExecutingAssembly().CodeBase.Substring(8);

private string GetSettingValue(string argSectionName, string argKey)
        {
            try
            {
Return GetReadOnlySetting(m_configFile, argSectionName, argKey);
            }
            catch (Exception ex)
            {
                WriteLogInfo.WriteLog(LogLevel.LV_ERROR, ex.ToString());
                return String.Empty;
            }
        }
///////////////////////////////////////////////////////////////////////////////
private static readonly string INIFILE = ".ini";
   private static Hashtable m_ReadOnlySetting = new Hashtable();

public static string GetReadOnlySetting(string argFileName, string argSection, string argKey)
        {
-------------------------------判断app.config文件是否存在----------------------
            if (m_ReadOnlySetting[argFileName] == null)
            {
                    if (m_ReadOnlySetting[argFileName] == null)
                    {
                        Hashtable htFile = new Hashtable();
                        m_ReadOnlySetting.Add(argFileName, htFile);
                    }
            }
            Hashtable file = m_ReadOnlySetting[argFileName] as Hashtable;
-------------------------------判断节点是否存在------------------------------
            if (file[argSection] == null)
            {
                    if (file[argSection] == null)
                    {
                        KeyValue keyValue = new KeyValue();
                        Hashtable htSection;
                        if (argFileName.EndsWith(INIFILE))
                        {
                            htSection = keyValue.GetIniSection(argFileName, argSection);
                        }
                        else
                        {
                            htSection = keyValue.GetValueList(argFileName, argSection);
                        }
                        file.Add(argSection, htSection);
                    }
            }
            Hashtable section = file[argSection] as Hashtable;
-------------------------------判断节点是否有值-----------------------------
            string strValue = section[argKey] as string;

            if (strValue == null || strValue == String.Empty)
            {
                throw new ArgumentNullException(String.Format("{0}的设定文件" +"<configuration><{1}><{2}>的构成不存在", argFileName, argSection, argKey));
            }
            return strValue;
        }
修改方式如下:

SetSettingValue(m_configFile, "directoryInfo", "savePath", _folderPath);
///////////////////////////////////////////////////////////////////////////////
public static void SetSettingValueHandle(string argFileName, string argSection, string argKey, string argValue)
        {
            try
            {
                Configuration config = ConfigurationManager.OpenExeConfiguration(argFileName);
                ConfigurationSection configSection = config.Sections[argSection];
                if (configSection != null)
                {
                    AppSettingsSection appSettingsSection = configSection as AppSettingsSection;
                    if (appSettingsSection != null && appSettingsSection.Settings.Count > 0)
                    {
                        appSettingsSection.Settings[argKey].Value = argValue;
                    }
                }

                config.Save();
            }
            catch (Exception ex)
            {
                throw new ArgumentNullException(String.Format("{0}的设定文件" +"<configuration><{1}><{2}>的构成不存在", argFileName, argSection, argKey), ex);
            }            
        }


--------------------编程问答-------------------- 不同的程序就算放在一起,配置文件也不会共享,所以你不可能做到让B读取A的配置文件,除非你还在使用一起的ini配置文件,那个是可以的。 --------------------编程问答-------------------- XML 读写时比较简单的 而且作为配置文件(扩展度很好)和数据交换文件是十分有用的 建议楼主一定要学! --------------------编程问答--------------------

string connectString = ConfigurationManager.ConnectionStrings["AName"].ConnectionString;
string connectString = ConfigurationManager.ConnectionStrings["BName"].ConnectionString;


--------------------编程问答-------------------- 路过............... --------------------编程问答--------------------

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Runtime.InteropServices;
namespace WindowsApplication1
{
  class Config
  {
    private Config() { }

    private static Config instance = new Config();
    
    internal static Config Instance
    {
      get { return Config.instance; }
      set { Config.instance = value; }
    }


    //写配置文件
    [DllImport("kernel32")]
    private static extern bool WritePrivateProfileString(
      string section,//段落
      string key,//小节点
      string val,//小节点的值
      string filePath);//配置文件的路径

    //读配置文件
    [DllImport("kernel32")]
    private static extern int GetPrivateProfileString(
      string section,//段落
      string key,//小节点
      string def,//读取失败时候的缺省值
      StringBuilder retVal,//读取的值
      int size,//读取值的大小
      string filePath);//配置文件的路径

    

    /// <summary>
    /// 创建新的配置项
    /// </summary>
    /// <param name="rootkey">段落</param>
    /// <param name="subkey">子节点</param>
    /// <param name="keyvalue">字节点的值</param>
    /// <returns>如果返回空则操作成功否则操作失败</returns>
    public string CreateNewConfig(string rootkey, string subkey, string keyvalue)
    {
      if (!File.Exists(Form1.ConfigFile))
        File.Create(Form1.ConfigFile).Close();
      if (!WritePrivateProfileString(rootkey, subkey, keyvalue, Form1.ConfigFile))
        return "创建段落失败!";
      return string.Empty;  
    }

    /// <summary>
    /// 更新指定节点的值
    /// </summary>
    /// <param name="rootkey">段落</param>
    /// <param name="subkey">子节点</param>
    /// <param name="keyvalue">字节点的值</param>
    /// <returns>如果返回空则操作成功否则操作失败</returns>
    public string UpdateConfig(string rootkey, string subkey, string keyvalue)
    {
      if (!File.Exists(Form1.ConfigFile))
        File.Create(Form1.ConfigFile).Close();
      if (!WritePrivateProfileString(rootkey, subkey, keyvalue, Form1.ConfigFile))
        return "修改关键值失败!";
      return string.Empty;
    }

    /// <summary>
    /// 读取指定节点的值
    /// </summary>
    /// <param name="rootkey">段落</param>
    /// <param name="subkey">子节点</param>
    /// <param name="keyvalue">字节点的值</param>
    /// <returns>如果返回空则操作成功否则操作失败</returns>
    public string ReadConfig(string rootkey, string subkey)
    {
      if (!File.Exists(Form1.ConfigFile))
        throw new Exception("没有找到配置文件!");
        //File.Create(ConfigFile).Close();
      StringBuilder RetVal = new StringBuilder();
      int readLe = GetPrivateProfileString(rootkey, subkey, "读取配置信息失败!", RetVal, 1024, Form1.ConfigFile);
      if (readLe != RetVal.Length)
        return "Error";
      return RetVal.ToString();
    }


  }
}



刚写的,还是热的! --------------------编程问答-------------------- 是不是读取a的配置文件写入b自己的配置文件? --------------------编程问答-------------------- 這事簡單的XMl操作問題!
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,