Not Monitored
Tag not monitored by Microsoft.
41,672 questions
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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);
}
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.