2014-11-01 43 views
3

我有一個.NET Windows窗體應用程序來顯示圖像,它訪問ConfigurationManager.AppSettings以獲取一些設置。ConfigurationManager.AppSettings - 打開時返回null

它在調試時工作正常,而且當我直接單擊我編譯的exe文件時,我可以從app.config文件中獲得我的設置。

問題出在我的應用程序不是由我直接執行的時候。當我將文件擴展名與它關聯時,或者手動執行「打開方式」我獲得空值,訪問ConfigurationManager.AppSettings

任何想法?

謝謝你這麼多

****當我用下面的代碼編輯*****

我發現了問題發生的情況:基本上

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

哪做文件關聯。我不明白什麼是根本原因,但在Explorer上手動執行文件關聯不會發生問題。

感謝

+0

有沒有明顯的方法,這應該失敗,Windows仍然運行您的.exe以同樣的方式調試程序。但是,再次,不使用'Properties.Settings'來檢索應用程序設置是非常*不常見的。意味着讓你應該像這樣的麻煩。你做錯了什麼,我們看不到你做錯了。 – 2014-11-01 12:44:16

+0

您可以檢查應用程序目錄中配置文件的名稱嗎?配置名稱應該是exename.exe.config,應用程序名稱應該是exename.exe。 – 2014-11-03 07:42:50

+0

嗨@ChaturvediDewashish我更新了我的問題,謝謝你的幫助。 – 2014-11-04 10:07:27

回答

0

我想這和它的工作

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

    } 
}