Anteux

Exportar componente

Apr 14th, 2016
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.58 KB | None | 0 0
  1. using System;
  2. using System.Diagnostics;
  3. using System.IO;
  4. using System.Reflection;
  5. using System.Windows;
  6.  
  7. namespace Haiser.Classes.Utilidades
  8. {
  9.     public static class FileExport
  10.     {
  11.         public static void ExportarFormulario()
  12.         {
  13.             try
  14.             {
  15.                 string finalpath = (System.Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + "\\PlanillaSolicitudEnap.xls");
  16.                 FileInfo file = new FileInfo(finalpath);
  17.  
  18.                 if (IsFileinUse(file))
  19.                 {
  20.                     throw new IOException("El archivo parece estar siendo utilizado por otra aplicación, verifique que no tiene abierto el formulario 'PlanillaSolicitudEnap.xls'");
  21.                 }
  22.  
  23.                 /////
  24.  
  25.                 if (File.Exists(System.Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + "\\PlanillaSolicitudEnap.xls"))
  26.                 {
  27.                     File.Delete(System.Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + "\\PlanillaSolicitudEnap.xls");
  28.                 }
  29.  
  30.                 using (Stream input = Assembly.GetExecutingAssembly().GetManifestResourceStream("Haiser.PlanillaSolicitudEnap.xls"))
  31.                 using (Stream output = File.Create("PlanillaSolicitudEnap.xls"))
  32.                 {
  33.                     CopyStream(input, output);
  34.                 }
  35.                 string _in = (AppDomain.CurrentDomain.BaseDirectory + "PlanillaSolicitudEnap.xls");
  36.                 string _out = (System.Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + "\\PlanillaSolicitudEnap.xls");
  37.                 File.Move(_in, _out);
  38.  
  39.                 if(MessageBox.Show("El formulario ha sido descargado a su Escritorio\nPor favor complételo antes de continuar", "Información", MessageBoxButton.OK, MessageBoxImage.Information) == MessageBoxResult.OK)
  40.                 {
  41.                     Process.Start(_out);
  42.                 }
  43.             }
  44.             catch(IOException ex)
  45.             {
  46.                 MessageBox.Show(ex.Message, "Error de procedimiento", MessageBoxButton.OK, MessageBoxImage.Warning);
  47.             }
  48.             catch (Exception)
  49.             {
  50.                 //MessageBox.Show(ex.ToString() + ", por favor comuníquese con el autor para resolverlo", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
  51.             }
  52.         }
  53.  
  54.         private static void CopyStream(Stream input, Stream output)
  55.         {
  56.             if(input == null)
  57.             {
  58.                 throw new Exception("Error de salida");
  59.             }
  60.  
  61.             // Insert null checking here for production
  62.             byte[] buffer = new byte[8192];
  63.             int bytesRead;
  64.             while ((bytesRead = input.Read(buffer, 0, buffer.Length)) > 0)
  65.             {
  66.                 output.Write(buffer, 0, bytesRead);
  67.             }
  68.         }
  69.  
  70.         private static bool IsFileinUse(FileInfo file)
  71.         {
  72.             FileStream stream = null;
  73.  
  74.             try
  75.             {
  76.                 stream = file.Open(FileMode.Open, FileAccess.ReadWrite, FileShare.None);
  77.             }
  78.             catch (IOException)
  79.             {
  80.                 //the file is unavailable because it is:
  81.                 //still being written to
  82.                 //or being processed by another thread
  83.                 //or does not exist (has already been processed)
  84.                 return true;
  85.             }
  86.             finally
  87.             {
  88.                 if (stream != null)
  89.                     stream.Close();
  90.             }
  91.             return false;
  92.         }
  93.     }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment