您好我正在使用WPF平台。我想执行文件操作,如解压缩和按下按钮移动文件。我希望按下按钮时的程序运行UnzipAndCreateUpdatePackage
方法,然后等待操作完成,然后运行Move Method
,然后向我提供确认。
我想要执行的操作: 使程序等待直到解压缩完成,然后从解压缩的文件夹中移动数据。由于解压缩过程没有完成,当前程序为我提供了一个未找到文件的例外。
我怎样才能实现这个目标
目前我的代码如下所示:按钮点击事件代码:
private void Button_Click(object sender, RoutedEventArgs e)
{
List<string> FileNames = new List<string>();
FileNames.Add("ApplicationData.zip");
FileNames.Add("MapData.zip");
FileNames.Add("LicenseData.zip");
WithoutExtentionNameList = new List<string>();
WithoutExtentionNameList.AddRange(FileNames);
for (int item = 0; item < WithoutExtentionNameList.Count; item++)
{
WithoutExtentionNameList[item] = WithoutExtentionNameList[item].Replace(".zip", " ").TrimEnd();
}
UnZipAndCreateUpdatePackage(FileNames);
moveData("LicenseTest 7.txt");
}
UnZipAndCreateUpdatePackage方法代码:
private void UnZipAndCreateUpdatePackage(List<string> FileNameList)
{
string CheckPreviousData = System.IO.Path.Combine(fileDestination, WithoutExtentionNameList[0]);
if (System.IO.Directory.Exists(CheckPreviousData))
{
DeleteDirectory(CheckPreviousData);
}
if (!System.IO.Directory.Exists(CheckPreviousData))
{
System.IO.Directory.CreateDirectory(CheckPreviousData);
}
foreach (var FileName in FileNameList)
{
if (FileName.Contains("Map"))
{
fileDestination = System.IO.Path.Combine(fileDestination, WithoutExtentionNameList[0]);
}
//create a source file path
sourceFile = System.IO.Path.Combine(fileLocation, FileName);
ZipFile.ExtractToDirectory(sourceFile, fileDestination);
}
fileDestination = @"C:\Users\adity\Desktop\MapUpdaterTemp";
}
Movedata方法:
private void moveData(string LicenseFileName)
{
//Now Map the file data as per Update Package
string MoveFrom = System.IO.Path.Combine(fileDestination, WithoutExtentionNameList[0], WithoutExtentionNameList[2], LicenseFileName);
string MoveTo = System.IO.Path.Combine(fileDestination, WithoutExtentionNameList[0], WithoutExtentionNameList[1], LicenseFileName);
System.IO.File.Move(MoveFrom, MoveTo);
}
答案 0 :(得分:0)
创建一个名为bool extractNow = fase;
的全局布尔值然后只需将while()语句添加到两个看起来像这样的键之间的代码中。
Stopwatch sw = new Stopwatch();
sw.Start();
UnZipAndCreateUpdatePackage(FileNames);
while(!extractNow) //or extractNow == false
{
}
sw.Stop();
TimeSpan milSecs = TimeSpan.FromMilliseconds(sw.ElapsedMilliseconds);
string timePassed = string.Format("{0:D2}d {1:D2}h:{2:D2}m:{3:D2}s",
milSecs.Days, milSecs.Hours, milSecs.Minutes, milSecs.Seconds);
moveData("LicenseTest 7.txt");
MessageBox.Show("Extraction Completed in :" + timePassed.ToString());
然后在提取结束时将bool添加为true。
private void UnZipAndCreateUpdatePackage(List<string> FileNameList)
{
string CheckPreviousData = System.IO.Path.Combine(fileDestination, WithoutExtentionNameList[0]);
if (System.IO.Directory.Exists(CheckPreviousData))
{
DeleteDirectory(CheckPreviousData);
}
if (!System.IO.Directory.Exists(CheckPreviousData))
{
System.IO.Directory.CreateDirectory(CheckPreviousData);
}
foreach (var FileName in FileNameList)
{
if (FileName.Contains("Map"))
{
fileDestination = System.IO.Path.Combine(fileDestination, WithoutExtentionNameList[0]);
}
//create a source file path
sourceFile = System.IO.Path.Combine(fileLocation, FileName);
ZipFile.ExtractToDirectory(sourceFile, fileDestination);
}
fileDestination = @"C:\Users\adity\Desktop\MapUpdaterTemp";
extractNow =false;
}
它不漂亮,但它应该有用。