How to open file saved in byte array?

Takeshi 41 Reputation points
2020-06-01T10:21:11.023+00:00

I am trying to open file saved in bytes. Here is my code ... how to do it correct?

public class Documents
        {
            public string name { get; set; }
            public string ext { get; set; }
            public long length { get; set; }
            public byte[] document { get; set; }
        }
        private void btnDocument_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                string init_path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments).ToString();

                F.OpenFileDialog file = new F.OpenFileDialog();
                file.Multiselect = true;
                file.Filter = "Image (*.png; *.jpg)|*.png; *.jpg|" +
                              "PDF file (*.pdf)|*.pdf|" +
                              "MS Word (*.doc; *.docx)|*.doc; *.docx|" +
                              "MS Excel (*.xls; *.xlsx)|*.xls; *.xlsx";
                file.DefaultExt = "*.pdf";
                file.InitialDirectory = init_path;
                long length = 0;
                byte[] array;
                if (file.ShowDialog() == F.DialogResult.OK)
                {
                    for (int i = 0; i < file.FileNames.Length; i++)
                    {
                        using (FileStream fs = File.OpenRead(file.FileNames[i]))
                        {
                            using (BinaryReader br = new BinaryReader(fs))
                            {
                                length = fs.Length;
                                array = br.ReadBytes((int)fs.Length);
                            }
                        }

                        Documents doc = new Documents();
                        doc.name = file.SafeFileNames[i].ToString();
                        doc.ext = Path.GetExtension(file.SafeFileNames[i].ToString());
                        doc.document = array;
                        doc.length = length;
                        dtgDocuments.Items.Add(doc);
                    }
                }
            }
            catch (Exception ex)
            { MessageBox.Show(ex.Message); }
        }

        private void dtgDocuments_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            byte[] array = (dtgDocuments.SelectedItem as Documents).document;
            FileStream file = File.Create("test_test");
            file.Write(array, 0, array.Length);
        }
Not Monitored
Not Monitored
Tag not monitored by Microsoft.
35,946 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Dave Patrick 426.1K Reputation points MVP
    2020-06-02T01:55:59.373+00:00

    Visual studio languages are not currently supported here on QnA. I'd try asking for help in dedicated forums here.

    https://social.msdn.microsoft.com/Forums/en-US/home?category=vslanguages

    --please don't forget to Accept as answer if the reply is helpful--


    Regards, Dave Patrick ....
    Microsoft Certified Professional
    Microsoft MVP [Windows Server] Datacenter Management

    Disclaimer: This posting is provided "AS IS" with no warranties or guarantees, and confers no rights.

    0 comments No comments