Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Diagnostics;
- using System.IO;
- using System.Reflection;
- using System.Windows;
- namespace Haiser.Classes.Utilidades
- {
- public static class FileExport
- {
- public static void ExportarFormulario()
- {
- try
- {
- string finalpath = (System.Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + "\\PlanillaSolicitudEnap.xls");
- FileInfo file = new FileInfo(finalpath);
- if (IsFileinUse(file))
- {
- throw new IOException("El archivo parece estar siendo utilizado por otra aplicación, verifique que no tiene abierto el formulario 'PlanillaSolicitudEnap.xls'");
- }
- /////
- if (File.Exists(System.Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + "\\PlanillaSolicitudEnap.xls"))
- {
- File.Delete(System.Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + "\\PlanillaSolicitudEnap.xls");
- }
- using (Stream input = Assembly.GetExecutingAssembly().GetManifestResourceStream("Haiser.PlanillaSolicitudEnap.xls"))
- using (Stream output = File.Create("PlanillaSolicitudEnap.xls"))
- {
- CopyStream(input, output);
- }
- string _in = (AppDomain.CurrentDomain.BaseDirectory + "PlanillaSolicitudEnap.xls");
- string _out = (System.Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + "\\PlanillaSolicitudEnap.xls");
- File.Move(_in, _out);
- 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)
- {
- Process.Start(_out);
- }
- }
- catch(IOException ex)
- {
- MessageBox.Show(ex.Message, "Error de procedimiento", MessageBoxButton.OK, MessageBoxImage.Warning);
- }
- catch (Exception)
- {
- //MessageBox.Show(ex.ToString() + ", por favor comuníquese con el autor para resolverlo", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
- }
- }
- private static void CopyStream(Stream input, Stream output)
- {
- if(input == null)
- {
- throw new Exception("Error de salida");
- }
- // Insert null checking here for production
- byte[] buffer = new byte[8192];
- int bytesRead;
- while ((bytesRead = input.Read(buffer, 0, buffer.Length)) > 0)
- {
- output.Write(buffer, 0, bytesRead);
- }
- }
- private static bool IsFileinUse(FileInfo file)
- {
- FileStream stream = null;
- try
- {
- stream = file.Open(FileMode.Open, FileAccess.ReadWrite, FileShare.None);
- }
- catch (IOException)
- {
- //the file is unavailable because it is:
- //still being written to
- //or being processed by another thread
- //or does not exist (has already been processed)
- return true;
- }
- finally
- {
- if (stream != null)
- stream.Close();
- }
- return false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment