I want to be able to load a config file (.config) of type System.Configuration.ConfigurationUserLevel using OpenFileDialog.
I need the file to be a ConfigurationUserLevel because the I need to use .AppSettings, as its functionality already exists in many other places throughout my code.
Currently I have,
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
var extension = Path.GetExtension(openFileDialog1.FileName);
if(extension.Equals(".config"))
{
try
{
var configFile = (ConfigurationUserLevel)openFileDialog1.OpenFile();
var settings = configFile.AppSettings.Settings;
but I get an error saying that I cannot simply convert from a Stream to ConfigurationUserLevel.
Is there a way for me to get a ConfigurationUserLevel file from an openFileDialog? Or is there a workaround?
OpenFileDialog
just helps users to getFileName
, of course you can't open file directly withopenFiledialog.OpenFile()
.You need to read file with
FileName
fromOpenFileDialog
, and parse it then cast toConfigurationUserLevel
.This can be help: https://msdn.microsoft.com/en-us/library/system.io.file(v=vs.110).aspx