20190705_關於winform程序修改程序名后, 報未將對象引用設置到對象的實例


winform做了一個小項目, 其中要用到數據庫連接, 字符串, 

   private string ConnStringSource =   System.Configuration.ConfigurationManager.ConnectionStrings["ConnStringSource"].ConnectionString;

 就直接用的app.config配置文件

開發完成后, 也沒有打包, 直接用debug下的文件發給用戶了, 用戶試用后, 覺得名字是英文的不方便, 想改下中文名

    , 這個圖是項目本來的名字;

 

 ,這個圖是用戶修改后的名字; 

改完名字之后, 就一直包下圖的經典錯誤:

解決辦法是:

將配置文件也對應的改名, 比如你將 PrintBindRFID.exe 修改為 RFID打印.exe , 那么配置文件也得改成 RFID打印.exe.config; 

問題到這里就解決了, 那么為什么是這樣的呢?

看看System.Configuration.ConfigurationManager.ConnectionStrings 類的源碼就知道了;

public static ConnectionStringSettingsCollection ConnectionStrings
{
    get
    {
        object obj2 = GetSection("connectionStrings");
        if ((obj2 == null) || (obj2.GetType() != typeof(ConnectionStringsSection)))
        {
            throw new ConfigurationErrorsException(SR.GetString("Config_connectionstrings_declaration_invalid"));
        }
        ConnectionStringsSection section = (ConnectionStringsSection) obj2;
        return section.ConnectionStrings; //配置字符串從這個方法來
    }
}
 
//-----------------------------------------------------------------------------------------
public static object GetSection(string sectionName)
{
    if (string.IsNullOrEmpty(sectionName))
    {
        return null;
    }
    PrepareConfigSystem();
    return s_configSystem.GetSection(sectionName);//從這里來
}

  private static void EnsureConfigurationSystem()
    {
        object obj2 = s_initLock;
        lock (obj2)
        {
            if (s_initState < 2)
            {
                s_initState = 1;
                try
                {
                    try
                    {
                        //s_configSystem使用這個進行實例化的, 轉到它的構造函數
                        s_configSystem = new ClientConfigurationSystem();
                        s_initState = 2;
                    }
                    catch (Exception exception)
                    {
                        s_initError = new ConfigurationErrorsException(SR.GetString("Config_client_config_init_error"), exception);
                        throw s_initError;
                    }
                }
                catch
                {
                    s_initState = 3;
                    throw;
                }
            }
        }
    }
//-----------------------------------------------------------------------------------------
  
internal ClientConfigurationSystem()
{
    this._configSystem = new ConfigSystem();
    //ClientConfigurationHost看下這個類里面
    this._configSystem.Init(typeof(ClientConfigurationHost), new object[2]);
    this._configHost = (ClientConfigurationHost) this._configSystem.Host;
    this._configRoot = this._configSystem.Root;
    this._configRoot.ConfigRemoved += new InternalConfigEventHandler(this.OnConfigRemoved);
    this._isAppConfigHttp = this._configHost.IsAppConfigHttp;
    string schemeDelimiter = Uri.SchemeDelimiter;
}
//-----------------------------------------------------------------------------------------
internal sealed class ClientConfigurationHost : DelegatingConfigHost, IInternalConfigClientHost
{
    // Fields
    private ClientConfigPaths _configPaths;
    private string _exePath;
    private ExeConfigurationFileMap _fileMap;
    private bool _initComplete;
    private const string ConfigExtension = ".config";
    internal const string ExeConfigName = "EXE";  //看到這里就不用再看下去了
    internal const string ExeConfigPath = "MACHINE/EXE";

 

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM