3

I know there are many threads on the subject, and I have try to implement (translate) this one into C# from VB.

Table fileContainer = {string FileName, binary File}

Here is my try:

partial void FileContainersAddAndEditNew_Execute()
{
    Dispatchers.Main.BeginInvoke(() =>
    {

        OpenFileDialog openDialog = new OpenFileDialog();

        if (openDialog.ShowDialog() == true)
        {
            using (System.IO.FileStream fileData = openDialog.File.OpenRead())
            {
                long fileLen = fileData.Length;

                if (fileLen > 0)
                {
                    Byte[] fileBArray = new Byte[fileLen];

                    fileData.Read(fileBArray, 0, fileLen);
                    fileData.Close();

                    FileContainer fc = this.FileContainers.AddNew();

                    fc.File = fileBArray;
                    fc.FileName = openDialog.File.Extension.ToString().ToLower();

                }
            }
        }

    });
}

But the code fails on this line:

 FileContainer fc = this.FileContainers.AddNew();

With this error:

IVisualCollection<T>.AddNew() should not be called from UI Thread.

I'm a bit confused. I thought the:

 Dispatchers.Main.BeginInvoke(() =>

prevented that from happening. Or am I doing it the wrong way?

Another thing I have notice is that the VB code uses the:

filenLen-1

but I get out of bounds trying to do that. They also don't cast it to an int but the .Read doesn't take a long as an argument.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Tim
  • 289
  • 3
  • 23
  • I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders Apr 19 '13 at 06:24
  • Thanks, i had missed that and will keep it in mind for later posts. – Tim Apr 19 '13 at 07:42

2 Answers2

4

openFileDialog.ShowDialog() does not return a bool value and cannot be used in an if statement like that. openFileDialog.ShowDialog() will open the dialog. As far as I know the execution pauses until you close the dialog and there's no reason to check if it's open or not.

filenLen-1

using (System.IO.FileStream fileData = openDialog.File.OpenRead());

long fileLen = fileData.Length;

should be

filenLen--;

using (System.IO.FileStream fileData = System.IO.File.OpenRead(openDialog.FileName))

int fileLen = int.Parse(fileData.Length.ToString());

fileData.Read(fileBArray, 0, fileLen); needs fileLen to be an integer. fileData returns a long for a reason though and this might cause problems.

As for the invoke question I'd have to know exactly what you're trying to do to be able to help you. Perhaps we can avoid invoking.

Hjalmar Z
  • 1,591
  • 1
  • 18
  • 36
  • Thanks for the help, i now think it works... I needed to invoke one time more (dont ask me why), that togheter with your input got the program to run. I havent downloaded any files from the DB, but the ".mdf" file get the added size of the file that i add so i hope it work. Do you see anything wrong with my code now (see my answer). Once again, thanks for your input :) – Tim Apr 12 '13 at 12:01
  • 2
    @Tim I can see a couple of things (in my eyes) wrong with your code, namely the ShowDialog() line and the OpenRead() line. I have never seen an OpenFileDialog used like that. However, if it works I'm not one to complain. Glad you figured it out. – Hjalmar Z Apr 15 '13 at 07:32
3

I post my updated code here to not mess up my orginal code for future readers. This should work;

    partial void FileContainersAddAndEditNew_Execute()
    {

        var supportedFiles = "*.*";
        Dispatchers.Main.BeginInvoke(() =>
        {
            OpenFileDialog openDialog = new OpenFileDialog();
            openDialog.Filter = "Supported files|" + supportedFiles;

            if (openDialog.ShowDialog() == true)
            {
                using (System.IO.FileStream fileData = openDialog.File.OpenRead())
                {
                    long fileLen = fileData.Length;

                    if (fileLen > 0)
                    {
                        Byte[] fileBArray = new Byte[fileLen--];

                        fileData.Read(fileBArray, 0, (int)fileLen);
                        fileData.Close();
                        var filename = openDialog.File.ToString().ToLower();

                        this.FileContainers.Details.Dispatcher.BeginInvoke(() =>
                        {
                            FileContainer fc = this.FileContainers.AddNew();
                            fc.File = fileBArray;
                            fc.FileName = filename;
                        });

                    }
                }
            }
        }); 

    }
Tim
  • 289
  • 3
  • 23