首页 > 代码库 > C# WPF 解压缩7zip文件 带进度条 sevenzipsharp
C# WPF 解压缩7zip文件 带进度条 sevenzipsharp
vs2013附件 :http://download.csdn.net/detail/u012663700/7427461
C# WPF 解压缩7zip文件 带进度条 sevenzipsharp
WPF PNG实现的图形进度条
.NET 3.5
http://sevenzipsharp.codeplex.com/
MainWindow.xaml
<Window x:Class="SevenZipTestWPF.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="SevenZipSharp WPF Demo" Height="157" Width="479" WindowStartupLocation="CenterScreen" >
<Grid>
<Image x:Name="image1" HorizontalAlignment="Left" Height="20" Margin="-13,25,0,0" VerticalAlignment="Top" Width="193" Source="progress-filled1.png" Stretch="None" RenderTransformOrigin="0.5,0.5">
<Image.RenderTransform>
<TransformGroup>
<ScaleTransform ScaleX="0.75" ScaleY="0.73"/>
<SkewTransform/>
<RotateTransform/>
<TranslateTransform/>
</TransformGroup>
</Image.RenderTransform>
</Image>
<Grid>
<ProgressBar Height="15" HorizontalAlignment="Left" Margin="10,10,0,0"
Name="progressBar1" VerticalAlignment="Top" Width="355" />
<Button Content="Extract" HorizontalAlignment="Left" Margin="383,10,0,0" Name="b_Extract" Width="52" Click="b_Extract_Click" VerticalAlignment="Top" />
<Label x:Name="label1" Content="Label" HorizontalAlignment="Left" Margin="10,50,0,0" VerticalAlignment="Top" Width="451"/>
</Grid>
</Grid>
</Window>
MainWindow.xaml.cs
using System;
using System.Windows;
using SevenZip;
namespace SevenZipTestWPF
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Check7zAndSevenZipSharpDll();
}
private string fileName = @"D:\Administrator\wwwroot\CPW\wmgjUpdate.7z";
private string directory = @"D:\Administrator\Desktop\完美世界国际版";
private string dll7z = AppDomain.CurrentDomain.BaseDirectory + "7z.dll";
private string dllSevenZipSharp = AppDomain.CurrentDomain.BaseDirectory + "SevenZipSharp.dll";
private int MaxValue = 0;
private int CurrentValue = 0;
private void b_Extract_Click(object sender, RoutedEventArgs e)
{
if (System.IO.File.Exists(fileName) && System.IO.Directory.Exists(directory))
{
SevenZipExtractor.SetLibraryPath(dll7z);
label1.Content = "开始更新客户端文件";
var extractor = new SevenZipExtractor(fileName);
progressBar1.Maximum = extractor.ArchiveFileData.Count;
MaxValue = extractor.ArchiveFileData.Count;
extractor.FileExtractionStarted += new EventHandler<FileInfoEventArgs>(extr_FileExtractionStarted);
extractor.BeginExtractArchive(directory);
}
}
void extr_FileExtractionStarted(object sender, FileInfoEventArgs e)
{
label1.Content = String.Format("更新文件 {0}", e.FileInfo.FileName);
progressBar1.Value += 1;
CurrentValue += 1;
ImageProgressBar(CurrentValue, MaxValue, 193, image1);
if (progressBar1.Maximum == progressBar1.Value)
label1.Content = "游戏更新完成";
}
private void Check7zAndSevenZipSharpDll()
{
bool b1 = System.IO.File.Exists(dll7z);
bool b2 = System.IO.File.Exists(dllSevenZipSharp);
if (!b1)
{
MessageBox.Show("7z.dll 不存在!", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
Close();
}
if (!b2)
{
MessageBox.Show("SevenZipSharp.dll 不存在!", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
Close();
}
}
/// <summary>
/// 图片进度条
/// </summary>
/// <param name="current"></param>
/// <param name="max"></param>
/// <param name="imageWidth">图片的实际宽度</param>
/// <param name="image1"></param>
private static void ImageProgressBar(double current, double max, double imageWidth, System.Windows.Controls.Image image1)
{
if (max != 0)
image1.Width = System.Math.Round((current / max) * imageWidth);
}
}
}
来自为知笔记(Wiz)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。