ConfigurationManager.AppSettings - Returns null when opened with
I have a Windows .NET Forms application to display images that are accessed to ConfigurationManager.AppSettings
get some customization.
It works great when debugging and also when I click on my compiled exe directly I can get my settings from the file app.config
.
The problem occurs when my application is not executed directly by me. When I associate a file extension with it, or manually do "Open With", I get zero access to ConfigurationManager.AppSettings
.
Any ideas?
Thank you very much
**** ***** EDIT
I found out that the problem occurs when I use the below code:
http://mel-green.com/2009/04/c-set-file-type-association/
Which mainly has to do with file association. I don't understand what is causing root, but doing file association manually in explorer does not happen.
thank
source to share
I tried this and it works
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";
}
}
}
source to share