【问题标题】:ConfigurationManager.AppSettings - Returns null on open withConfigurationManager.AppSettings - 打开时返回 null
【发布时间】:2014-11-01 11:35:44
【问题描述】:

我有一个 .NET Windows 窗体应用程序来显示图像,它访问 ConfigurationManager.AppSettings 以获取一些设置。

它在调试时运行良好,而且当我直接单击我编译的 exe 时,我可以从 app.config 文件中获取我的设置。

当我的应用程序不是由我直接执行时,就会出现问题。当我将文件扩展名与其关联时,或手动执行 “打开方式” 时,访问ConfigurationManager.AppSettings 时会得到空值。

有什么想法吗?

非常感谢

****编辑*****

当我使用以下代码时,我发现问题发生了:

http://mel-green.com/2009/04/c-set-file-type-association/

这基本上是文件关联。我不明白根本原因是什么,但是在资源管理器上手动进行文件关联不会发生问题。

谢谢

【问题讨论】:

  • 没有明显的方法会失败,Windows 仍然以与调试器相同的方式运行您的 .exe。但话又说回来,不使用Properties.Settings 来检索应用程序设置是非常不寻常的。打算让你遇到这样的麻烦。你做错了什么,我们看不到你做错了。
  • 您能检查一下应用程序目录中的配置文件的名称吗?配置名称应为 exename.exe.config,应用程序名称应为 exename.exe。
  • 嗨@ChaturvediDewashish 我更新了我的问题,感谢您的帮助。

标签: c# .net winforms


【解决方案1】:

我试过这个和它的工作原理

App.Config

 <?xml version="1.0" encoding="utf-8" ?>
<configuration>

  <appSettings>
    <add key="ExtensionSupported" value ="jpeg,jpg"/>
  </appSettings>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
</configuration>

Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            if(args.Length>0)
                Application.Run(new Form1(args[0]));
            else
            Application.Run(new Form1());
        }
    }
}

Form1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        private string p;

        public Form1()
        {
            InitializeComponent();



        }

        public Form1(string filePath):this()
        {
            MessageBox.Show("Loading file "+ filePath);
            CheckForAllowed(filePath);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            openFileDialog1.Multiselect = false;
            DialogResult dr=  openFileDialog1.ShowDialog();

            if (dr == System.Windows.Forms.DialogResult.OK)
                CheckForAllowed(openFileDialog1.FileName);

        }

        private void CheckForAllowed(string filePath)
        {
            string appSetting = ConfigurationManager.AppSettings["ExtensionSupported"];
            MessageBox.Show("Allowed file types are " + appSetting);
            string[] allowedFileTypes = appSetting.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

            string fileExtesnsion = filePath.Substring(filePath.LastIndexOf('.')+1).Trim();



            if (allowedFileTypes.Contains(fileExtesnsion))
                label1.Text = "Allowed";
            else
                label1.Text = "Not allowed";
        }

    }
}

【讨论】:

    猜你喜欢
    • 2013-08-30
    • 2018-10-02
    • 2014-03-08
    • 1970-01-01
    • 1970-01-01
    • 2014-08-10
    • 1970-01-01
    • 2021-11-10
    • 1970-01-01
    相关资源
    最近更新 更多