• 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";
    
     
    

      

  • 相关阅读:
    四个好看的CSS样式表格
    POJ 2255 Tree Recovery
    黑马程序猿_2014 7月 我使用多线程体验
    Dos命令将合并两个文本文件的内容
    栈和堆之间的差(他转身无数的文章)
    【Espruino】NO.12 加速度计演示
    MySQL进口.sql文件和常用命令
    typedef和define具体的具体差异
    muduo网络图书馆评测
    Web采矿技术
  • 原文地址:https://www.cnblogs.com/wxylog/p/11138520.html
Copyright © 2020-2023  润新知