-1

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?

  • Maybe this helps https://stackoverflow.com/a/506637/572332 And to get the path and filename from the dialog try openFileDialog1.FileName (https://msdn.microsoft.com/en-us/library/system.windows.forms.filedialog.filename(v=vs.110).aspx) – robor Feb 12 '18 at 16:29
  • @robor78 This worked, thank you! – ReddestHorse Feb 12 '18 at 16:41

1 Answers1

0

OpenFileDialog just helps users to get FileName, of course you can't open file directly with openFiledialog.OpenFile().

You need to read file with FileName from OpenFileDialog, and parse it then cast to ConfigurationUserLevel.

This can be help: https://msdn.microsoft.com/en-us/library/system.io.file(v=vs.110).aspx

Kangjun Heo
  • 1,023
  • 1
  • 8
  • 19