none
Filestream'ing a file in Properties.Resources RRS feed

  • Question

  • I'm building a GUI_FileDialog and its Designer tool.

    The designer-tool creates GUI file-dialogs and saves their appearances and details in a File-stream.

    Currently, to use any created-GUI-FileDialogs they are created during run-time just like the OpenFileDialo/SaveFileDialog native to C#.

    but to set their appearances/details to those recorded using the DesignerTool the Designer-Tool's output file needs to be loaded via a FileStream and the file's name.

    - I would like to add the GUI File-Dialog's description file to my project's resources and not rely on it being found on the user's hard-drive ...

    to use the GUI_FileDialog is simple :

    private void LbtnLoad_Click(object sender, EventArgs e)
    {
        OpenFileDialog_GUI ofd = new OpenFileDialog_GUI();
        ofd.GUI_Load(System.IO.Directory.GetCurrentDirectory() + "\\FileDialog_Tools.fdGui");
        ofd.Filter = "File Dialogs (*." + Ck_Objects.FileDialog_GUI.FileExtension + ")|*." + Ck_Objects.FileDialog_GUI.FileExtension;
        if (ofd.ShowDialog() == DialogResult.OK)
        {
            fdGUI.GUI_Load(ofd.FileName);
            loadFileDialog_Interface();
            fdGUI.Show();
            formGUI.instance.FileDialog = fdGUI;
        }
    }

    just like any other OpenFileDialog

    and the fdGUI.GUI_Load() function starts like this 

    public void GUI_Load(string strFileName)
    {
        cleanExtension(ref strFileName);
        FileStream fs = new FileStream(strFileName, FileMode.Open);
    
        //                                                              FORM
        //                              size
        Width = (int)formatter.Deserialize(fs);
        Height = (int)formatter.Deserialize(fs);
    // yada yada yada
    }

    to get results that look like this 

    because sometimes you ~want~  GUI-nea Pig!

    can someone tell me how to read my file-stream from my project's Properties.Resource ?

    Christ


    my code is perfect until i don't find a bug

    Tuesday, December 24, 2019 8:49 PM

Answers

  • If you add the file that contains your settings into a resx, then you can read the content as an array of bytes. If you pass the bytes to the constructor of a MemoryStream, then you have a Stream connected to the file in the resources:

                byte[] resourceBytes = Resource1.NameOfYourFile;
                MemoryStream ms = new MemoryStream(resourceBytes);
    

    Once you have a Stream in the variable "ms", you can deserialize your data from it in the same way as you are curently using to deserialize from a FileStream. The deserializer just uses a Stream, it doesn't care about what type of Stream it is, so you can pass either a FileStream or a MemoryStream and both will be read in the same way.

    • Marked as answer by Christ Kennedy Wednesday, December 25, 2019 2:43 PM
    Wednesday, December 25, 2019 8:32 AM
    Moderator

All replies

  • One way is to use the following which returns a Dictionary with name of text resource and contents of resource.

    public Dictionary<string, string> GetTextResources()
    {
        
        var resultDictionary = new Dictionary<string,string>();
        var properties = typeof(Resources)
            .GetProperties(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static);
    
        var stringItems = (from pi in properties
            where pi.PropertyType == typeof(string) select pi).ToList();
    
        if (stringItems.Count <= 0) return resultDictionary;
    
        foreach (var propertyInfo in stringItems)
        {
            resultDictionary.Add(
                propertyInfo.Name, 
                Resources.ResourceManager.GetObject(propertyInfo.Name)?.ToString());
        }
    
        return resultDictionary;
    
    }
    Source code, usage, part of a TechNet article.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Tuesday, December 24, 2019 11:55 PM
    Moderator
  • If you add the file that contains your settings into a resx, then you can read the content as an array of bytes. If you pass the bytes to the constructor of a MemoryStream, then you have a Stream connected to the file in the resources:

                byte[] resourceBytes = Resource1.NameOfYourFile;
                MemoryStream ms = new MemoryStream(resourceBytes);
    

    Once you have a Stream in the variable "ms", you can deserialize your data from it in the same way as you are curently using to deserialize from a FileStream. The deserializer just uses a Stream, it doesn't care about what type of Stream it is, so you can pass either a FileStream or a MemoryStream and both will be read in the same way.

    • Marked as answer by Christ Kennedy Wednesday, December 25, 2019 2:43 PM
    Wednesday, December 25, 2019 8:32 AM
    Moderator