Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.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# System.Configuration.ConfigurationSettings.AppSettings在生产环境中不可访问_C#_.net_Web Config_Desktop Application_Windows Applications - Fatal编程技术网

C# System.Configuration.ConfigurationSettings.AppSettings在生产环境中不可访问

C# System.Configuration.ConfigurationSettings.AppSettings在生产环境中不可访问,c#,.net,web-config,desktop-application,windows-applications,C#,.net,Web Config,Desktop Application,Windows Applications,我有一个windows桌面应用程序,web应用程序正从该应用程序启动 private void Home_Load(object sender, EventArgs e) { string url = string.Format("http://localhost:49916/Express/Login.aspx?yek@soh={0}", System.Configuration.ConfigurationSettings.AppSettings["HK"]

我有一个windows桌面应用程序,web应用程序正从该应用程序启动

private void Home_Load(object sender, EventArgs e)
       {
           string url = string.Format("http://localhost:49916/Express/Login.aspx?yek@soh={0}", System.Configuration.ConfigurationSettings.AppSettings["HK"].ToString());

           Process.Start("IExplore.exe", url);
           this.Close();
       }
我的机器运转良好。然后,我创建了一个安装程序来安装它,该程序运行良好,但是,当我在生产机器上运行新安装的程序时,出现以下异常:

System.NullReferenceException: Object reference not set to an instance of an object.
   at HospitalClient_App.Home.Home_Load(Object sender, EventArgs e)
   at System.Windows.Forms.Form.OnLoad(EventArgs e)
   at System.Windows.Forms.Form.OnCreateControl()
   at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
   at System.Windows.Forms.Control.CreateControl()
   at System.Windows.Forms.Control.WmShowWindow(Message& m)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
   at System.Windows.Forms.ContainerControl.WndProc(Message& m)
   at System.Windows.Forms.Form.WmShowWindow(Message& m)
   at System.Windows.Forms.Form.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
如果我换线

 Process.Start("IExplore.exe", url); 

然后程序就开始工作了

我的app.config如下所示:

<configuration>
    <appSettings>
        <add key="HK" value="PRO2"/>
        <add key="ClientSettingsProvider.ServiceUri" value=""/>
    </appSettings>
    <startup>
        <supportedRuntime version="v2.0.50727"/>
    </startup>
</configuration>


我如何解决这个问题?什么可能导致问题?

我的心理调试器看到潜在NRE的唯一地方是:

ConfigurationSettings.AppSettings["HK"].ToString()
(也就是说,使用
ConfigurationManager.AppSettings[]

它在那里抛出的事实将告诉您,找不到键为
HK
的Appsetting,从而导致
ConfigurationSettings.AppSettings[“HK”]
返回
null
,导致
null.ToString()
抛出上述异常


确保相关配置的appsettings部分中有一个

是否可能是您查看了错误的app.config?我已经做了100次了

在运行时,正在读取的配置文件的文件名不是app.config。它是applicationname.extension.config


例如:如果您正在运行application.exe,则配置文件的名称将为application.exe.config。该文件应与应用程序位于同一目录中

你的app.config的appSettings部分是什么样子的?这是我的app.config文件谢谢你编辑我的问题。。如果你能帮助我那就太好了。你不需要运气,只需要逻辑。你没告诉我出了什么问题。您不知道配置是否包含正确的应用程序设置。这是我的app.config文件,但你想从网站上阅读它。它不应该是
web.config
?通过编辑您的问题,更好地解释您的问题,解释什么项目安装在哪里,它们使用什么配置文件以及这些文件的命名方式。对于我问题中的歧义,我深表歉意。1.密钥位于App.Config(Windows应用程序)2中。我试图在Home.cs的加载事件中读取它(这是windows应用程序中的启动窗体)。private void Home_Load(object sender,EventArgs e){string url=string.Format(“{0}”,System.Configuration.ConfigurationSettings.AppSettings[“HK”].ToString());Process.Start(“IExplore.exe”,url);}上述代码在开发过程中运行良好(VS 2008)。但当安装程序创建、安装并尝试运行时。它在读取appsettings键的行抛出错误。。这就是问题所在。配置是否与应用程序位于同一文件夹中?它的名字正确吗?applicationname.extension.config?调试时,调试器将读取vhost配置文件,而不是applicationname.extension.config文件。@user1540857调试时OP没有问题。Visual studio将把app.config复制到vhost。。。根据需要归档。听起来好像另一台机器上的安装程序没有创建正确的文件。
ConfigurationSettings.AppSettings["HK"].ToString()