The base stream is not writable (GZipStream) - C#

Узнай цену своей работы

Формулировка задачи:

Привет всем. Мне нужно через некоторый период времени добавлять в архив файлы. Делаю это так:
Листинг программы
  1. compStream = new GZipStream(destFile, CompressionMode.Compress);
  2. sourceFile = File.OpenRead(@"C:\Temp\filename);
  3. try
  4. {
  5. int value = sourceFile.ReadByte();
  6. while (value != -1)
  7. {
  8. compStream.WriteByte((byte)value);
  9. value = sourceFile.ReadByte();
  10. }
  11. sourceFile.Dispose();
  12. compStream.Dispose();
  13. compStream.Close();
  14. File.Delete(@"C:\Temp\filename);
  15. }
  16. catch { }
После первого прохода таймера файл архивируется, но на второй такт таймера - ошибка : The base stream is not writable на
Листинг программы
  1. compStream = new GZipStream(destFile, CompressionMode.Compress);
Как решить проблему?

Решение задачи: «The base stream is not writable (GZipStream)»

textual
Листинг программы
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.IO;
  7. using System.IO.Compression;
  8. using System.Linq;
  9. using System.Net;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using System.Windows.Forms;
  13.  
  14. namespace Screener
  15. {
  16.     public partial class Form1 : Form
  17.     {
  18.         public Form1()
  19.         {
  20.             InitializeComponent();
  21.         }
  22.         public Image ScrImage;
  23.         public string filename = SystemInformation.UserName.ToString() + "_" + Environment.MachineName.ToString();
  24.         FileStream destFile;
  25.         FileStream sourceFile;
  26.         GZipStream compStream ;
  27.         private void Form1_Load(object sender, EventArgs e)
  28.         {
  29.            
  30.                 if (File.Exists(@"C:\Temp\Screener\zip.zip"))
  31.                 {
  32.                    
  33.                     DirectoryInfo dirInfo = new DirectoryInfo(@"C:\Temp\Screener\");
  34.                     foreach (FileInfo file in dirInfo.GetFiles())
  35.                     {
  36.                         file.Delete();
  37.                     }
  38.                     destFile = File.Create(@"C:\Temp\Screener\zip.zip");
  39.                    
  40.                 }
  41.                 else
  42.                 {
  43.                     Directory.CreateDirectory(@"C:\Temp\Screener");
  44.                     destFile = File.Create(@"C:\Temp\Screener\zip.zip");
  45.                    
  46.                 }
  47.                  
  48.             }
  49.        
  50.         public Bitmap ImageFromScreen()
  51.         {
  52.             Bitmap bmp = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
  53.             using (var gr = Graphics.FromImage(bmp))
  54.             {
  55.                 gr.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y,
  56.                     0, 0, Screen.PrimaryScreen.Bounds.Size);
  57.             }
  58.             return bmp;
  59.         }
  60.        
  61.  
  62.         private void timer1_Tick(object sender, EventArgs e)
  63.         {
  64.             compStream = new GZipStream(destFile, CompressionMode.Compress);  
  65.             string datescr = System.DateTime.Now.ToString().Replace(":", ".");
  66.  
  67.             ScrImage = ImageFromScreen();
  68.             ScrImage.Save(@"C:\Temp\Screener\" + datescr + filename, System.Drawing.Imaging.ImageFormat.Jpeg);
  69.             ScrImage.Dispose();
  70.             sourceFile = File.OpenRead(@"C:\Temp\Screener\" + datescr + filename);
  71.             try
  72.             {
  73.                 int value = sourceFile.ReadByte();
  74.                 while (value != -1)
  75.                 {
  76.                     compStream.WriteByte((byte)value);
  77.                     value = sourceFile.ReadByte();
  78.                 }
  79.                 sourceFile.Dispose();
  80.                 compStream.Dispose();
  81.                 compStream.Close();
  82.                 File.Delete(@"C:\Temp\Screener\" + datescr + filename);
  83.             }
  84.             catch(Exception ex) {MessageBox.Show(ex.ToString()); }
  85.         }
  86.  
  87.  
  88.         private void Form1_FormClosing(object sender, FormClosingEventArgs e)
  89.         {
  90.            
  91.         }
  92.  
  93.        
  94.     }
  95. }

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

6   голосов , оценка 4 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут
Похожие ответы