2011-12-22 117 views
0

我需要在我的項目中包含一個進度條,但我不確定如何做或調用更新的方法有多成功。 ,我想包括代碼如下,非常感謝你:如何將進度條添加到我在C#中的SharpZipLib實用程序中

using System; 
using System.Collections; 
using System.Text; 
using System.IO; 
using System.ComponentModel; 
using System.Windows.Forms; 
using System.Management; 
using ICSharpCode.SharpZipLib.Checksums; 
using ICSharpCode.SharpZipLib.Zip; 
using ICSharpCode.SharpZipLib.GZip; 


namespace PCCoste 
{ 
    public partial class FormBackup : Form 
    { 
     private string rutaDestinoCopia; 
     private string nombreArchivo; 
     private Timer time = new Timer(); 

     //static NameValueCollection configItems; 

     public FormBackup() 
     { 
      InitializeComponent(); 
      if (File.Exists("backup.ini")) 
      { 
       StreamReader sr = new StreamReader("backup.ini"); 
       rutaDestinoCopia = sr.ReadLine(); 
       nombreArchivo = sr.ReadLine(); 
       sr.Close(); 
       txtRutaDestino.Text = rutaDestinoCopia; 
       txtNombreArchivo.Text = nombreArchivo; 
      } 
     } 

     private void botonDestino_Click(object sender, EventArgs e) 
     { 
      FolderBrowserDialog expArchivos = new FolderBrowserDialog(); 
      expArchivos.RootFolder = Environment.SpecialFolder.MyComputer; 
      expArchivos.ShowNewFolderButton = true; 
      expArchivos.Description = "Seleccione unidad o carpeta de destino para realizar la copia de seguridad."; 

      if (expArchivos.ShowDialog(this) != DialogResult.Cancel) 
      { 
       rutaDestinoCopia = expArchivos.SelectedPath; 
       txtRutaDestino.Text = rutaDestinoCopia; 

      } 
     } 

     private void botonBackup_Click(object sender, EventArgs e) 
     { 
      StreamWriter escribeArchivo = new StreamWriter("backup.ini"); 
      escribeArchivo.WriteLine(txtRutaDestino.Text); 
      escribeArchivo.WriteLine(txtNombreArchivo.Text); 
      escribeArchivo.Close(); 
      Zip("C:\\Users\\Andrés\\Desktop\\PCCoste\\PCCoste\\bbdd", 
       rutaDestinoCopia + "\\" + nombreArchivo + "-" + DateTime.Now.ToString("ddMMyyyy") + ".zip", 
       txtContraseña.Text); 
     } 

     public static void Zip(string Path, string outPathAndZipFile, string password) 
     { 
      string OutPath = outPathAndZipFile; 
      ArrayList lista = GenerateFileList(Path); // Genera la listade archivos. 

      int TrimLength = (Directory.GetParent(Path)).ToString().Length; 
      TrimLength += 1; //borra '\' 
      FileStream flujoArchivos; 
      byte[] obuffer; 
      ZipOutputStream ZipStream = new ZipOutputStream(System.IO.File.Create(OutPath)); // creamos el zip 
      ZipStream.SetLevel(9); // 9 = nivel de compresión máxima. 

      if (password != String.Empty) ZipStream.Password = password; 

      ZipEntry ZipEntrada; 
      foreach (string archivo in lista) // para cada archivo genera una entrada zip 
      { 
       ZipEntrada = new ZipEntry(archivo.Remove(0, TrimLength)); 
       ZipStream.PutNextEntry(ZipEntrada); 

       if (!archivo.EndsWith(@"/")) // si el archivo acaba es '/' es que es una carpeta 
       { 
        flujoArchivos = File.OpenRead(archivo); 
        obuffer = new byte[flujoArchivos.Length]; // buffer 
        flujoArchivos.Read(obuffer, 0, obuffer.Length); 
        ZipStream.Write(obuffer, 0, obuffer.Length); 
        Console.Write("."); 
        flujoArchivos.Close(); 
       } 
      } 
      ZipStream.Finish(); 
      ZipStream.Close(); 
      MessageBox.Show("Copia terminada con éxito", "Información", MessageBoxButtons.OK, MessageBoxIcon.Information); 
     } 

     private static ArrayList GenerateFileList(string Dir) 
     { 
      ArrayList mid = new ArrayList(); 
      bool Empty = true; 
      foreach (string file in Directory.GetFiles(Dir)) // añada cada archivo al directorio 
      { 
       mid.Add(file); 
       Empty = false; 
      } 

      if (Empty) 
      { 
       if (Directory.GetDirectories(Dir).Length == 0) // si la carpeta está vacía la copia también. 
       { 
        mid.Add(Dir + @"/"); 
       } 
      } 
      foreach (string dirs in Directory.GetDirectories(Dir)) // do this recursively 
      { 
       // configurar las carpetas excluidas. 
       string testDir = dirs.Substring(dirs.LastIndexOf(@"\") + 1).ToUpper(); 
       //if (FormBackup.excludeDirs.Contains(testDir)) 
       // continue; 
       foreach (object obj in GenerateFileList(dirs)) 
       { 
        mid.Add(obj); 
       } 
      } 
      return mid; // devuelve la lista de archivos 
     } 

     private void checkMostrarConstraseña_CheckedChanged(object sender, EventArgs e) 
     { 
      if (checkMostrarConstraseña.Checked) 
       txtContraseña.UseSystemPasswordChar = false; 
      else 
       txtContraseña.UseSystemPasswordChar = true; 
     } 

     private void botonCancelarBackup_Click(object sender, EventArgs e) 
     { 
      Close(); 
     } 
    } 
} 
+0

我試圖計算在foreach(列表中的字符串archivo)中的字節流,並更新欄,但不起作用。 :( – chustron 2011-12-22 14:38:41

回答

0

看一看周圍,涉及在後臺線程運行的操作的一些解決方案。您可能需要在另一個線程上執行ZIP操作(如果操作冗長或全部在一個方法調用中發生),並且只要您認爲ZIP操作內發生了一些進度,就可以通過回調到UI線程來更新UI。

+0

對不起,但我不明白你的意思。我試圖計數在foreach中的字節流(列表中的字符串archivo)並更新欄但不起作用:( – chustron 2011-12-22 14:40:56

+1

這可能是因爲你正在運行在UI線程上(與進度條相同),所以當你打電話更新進度條時,它將不會實際更新UI,直到你的方法完成(在這一點上,你將完成ZIP操作))。看看[這](http://stackoverflow.com/questions/5268260/c-sharp-how-to-properly-use-progress-bars-guis)問題的例子,你應該是什麼四處尋找 – 2011-12-22 15:01:51

+0

謝謝!我會看看,我會公佈我的結果。:) – chustron 2011-12-22 15:11:50

相關問題