保持一个文件操作,直到一个特定的文件操作完成
本文关键字:一个 文件 操作 | 更新日期: 2023-09-27 18:05:11
你好,我在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);
}
创建一个全局布尔值,命名为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;
}