File generation binary stream, binary stream generation file

Source: Internet
Author: User

  1. #region Convert binary to file
  2. public static string Convertbytetofile (object objdata, string filepathname)
  3. {
  4. //string fileName = "";
  5. //filename = new Publicconst ().  Pathtempfile + fileName;
  6. String folder = System.IO.Path.GetDirectoryName (filepathname);
  7. if (! System.IO.Directory.Exists (folder))
  8. {
  9. System.IO.Directory.CreateDirectory (folder);
  10. }
  11. if (System.IO.File.Exists (filepathname))
  12. {
  13. Try
  14. {
  15. System.IO.File.Delete (filepathname);
  16. }
  17. Catch
  18. {
  19. //("Fileinuse");
  20. return "";
  21. }
  22. }
  23. System.IO.FileStream fs = new System.IO.FileStream (Filepathname, System.IO.FileMode.Create,  System.IO.FileAccess.Write);
  24. System.IO.BinaryWriter w = new System.IO.BinaryWriter (FS);
  25. Try
  26. {
  27. W.write (Objdata as byte[]);
  28. }
  29. catch (Exception)
  30. {
  31. }
  32. finally
  33. {
  34. W.close ();
  35. Fs. Close ();
  36. }
  37. return filepathname;
  38. }
  39. #endregion

  1. #region convert files into binary
  2. public static byte[] Convertfiletobyte (string fileName)
  3. {
  4. if (! System.IO.File.Exists (fileName))
  5. {
  6. return null;
  7. }
  8. System.IO.FileStream fs = new System.IO.FileStream (FileName, System.io.filemode.open,system.io.fileaccess.read,  Fileshare.readwrite);
  9. byte[] Nowbyte = new byte[(int) fs.  Length];
  10. Try
  11. {
  12. Fs. Read (nowbyte, 0, (int) fs.  Length);
  13. return nowbyte;
  14. }
  15. catch (Exception)
  16. {
  17. return null;
  18. }
  19. finally
  20. {
  21. Fs. Close ();
  22. }
  23. }
  24. #endregion

File generation binary stream, binary stream generation file

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.