c# - 读取配置文件在另一台计算机上不起作用

标签 c# .net app-config windows-applications

我正在用 C# 制作 Windows 应用程序。我在 .NET Framework 3.5 中制作了一个应用程序,我正在使用 Windows Vista。当我正在构建该应用程序并尝试在其他装有 Windows 7 的机器上运行时,它无法正常工作。目标机器在控制面板中显示 .NET Framework 4.0 客户端配置文件。每当我尝试从 CONFIG 文件中读取一些值时,就会出现此问题。如果我不使用配置文件,那么我的应用程序将运行,但我必须使用配置文件。 为了检查框架问题,我做了一个简单的应用程序,我试图在单击按钮时读取配置文件值作为 MessageBox.Show(ConfigurationManager.AppSettings["FlagForArchiving"].ToString()); 但我收到错误

See the end of this message for details on invoking 
just-in-time (JIT) debugging instead of this dialog box.

************** Exception Text **************
System.NullReferenceException: Object reference not set to an instance of an object.
   at TestFramework.Form1.button1_Click(Object sender, EventArgs e)
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ButtonBase.WndProc(Message& m)
   at System.Windows.Forms.Button.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)


************** Loaded Assemblies **************
mscorlib
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.239 (RTMGDR.030319-2300)
    CodeBase: file:///C:/Windows/Microsoft.NET/Framework/v4.0.30319/mscorlib.dll
----------------------------------------
TestFramework
    Assembly Version: 1.0.0.0
    Win32 Version: 1.0.0.0
    CodeBase: file:///C:/Users/mvi/AppData/Local/Temp/Temp1_test.zip/Release/TestFramework.exe
----------------------------------------
System.Windows.Forms
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.235 built by: RTMGDR
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Windows.Forms/v4.0_4.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
----------------------------------------
System.Drawing
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.1 built by: RTMRel
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Drawing/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
----------------------------------------
System
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.236 built by: RTMGDR
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System/v4.0_4.0.0.0__b77a5c561934e089/System.dll
----------------------------------------
System.Configuration
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.1 (RTMRel.030319-0100)
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Configuration/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll
----------------------------------------
System.Xml
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.233 built by: RTMGDR
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Xml/v4.0_4.0.0.0__b77a5c561934e089/System.Xml.dll
----------------------------------------

************** JIT Debugging **************
To enable just-in-time (JIT) debugging, the .config file for this
application or computer (machine.config) must have the
jitDebugging value set in the system.windows.forms section.
The application must also be compiled with debugging
enabled.

For example:

<configuration>
    <system.windows.forms jitDebugging="true" />
</configuration>

When JIT debugging is enabled, any unhandled exception
will be sent to the JIT debugger registered on the computer
rather than be handled by this dialog box.

请帮助我解决我的问题。提前致谢。

最佳答案

FileNotFoundException 表示配置文件不在您的应用程序文件夹中。确保在发布应用程序时部署配置文件。

是的,您需要在每台安装您应用的机器上都有一个配置文件。

关于c# - 读取配置文件在另一台计算机上不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8208451/

相关文章:

nhibernate - 如何将应用程序设置加载到 NHibernate.Cfg.Configuration 对象?

c# - LINQ - 包含匿名类型

c# - 从 dll 添加鼠标悬停摘要

.net - TeamCity - MSBuild 运行程序退出时代码不为零

ASP.NET - 多个站点共享同一个数据库,如何管理用户名和密码?

.net - <appSettings/> 是否可以包含设置并引用不同文件中的另一个 <appSettings/> 部分?

c# - C#中replace方法的问题

C# Property Grid属性选择多次显示表单

c# - 使用 EventTrigger 引发附加事件

c# - 如何管理多个不断增长的列表?