Question

I want to create a program which opens my file onClick by providing its content in byte[] format in new page.

Please help.

Was it helpful?

Solution

The OpenFileDialog provides this capability, and it works the same in Silverlight versions from 2 through 4.

Here's a simple function that reads the bytes into a byte array for you.

http://msdn.microsoft.com/en-us/library/system.windows.controls.openfiledialog(VS.95).aspx

OpenFileDialog ofd = new OpenFileDialog()
{
    Multiselect = false,
};
if (ofd.ShowDialog() == true)
{
    FileInfo file = ofd.File;
    byte[] bytes;
    using (FileStream fs = file.OpenRead())
    {
        bytes = new byte[fs.Length];
        int l = (int)fs.Length;
        int r = 0;
        while (l > 0)
        {
            int read = fs.Read(bytes, r, l);
            if (read != 0)
            {
                r += read;
                l -= read;
            }
        }
    }

    // All the bytes of the file are now in the "bytes" array
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top