MEFを使用してアセンブリを読み込むWinFormsプログラムを作成しています。これらのアセンブリは、実行可能ファイルと同じフォルダにありません。 ファイルメンテナンスを行う必要があるため、実際のWinFormをロードする前にProgram.csファイルにいくつかのコードを実装していますので、プログラムによって(アセンブリであっても)ファイルがロードされません。ファイルを操作してブロックする
が、私は2つの操作を実行しています: - 他の1つの に一つの場所からフォルダを移動する - アーカイブからファイルがある場合は、フォルダからdllファイルをアーカイブから解凍ファイルや上書き移動(1よりも新しいが移動しました)
問題は、フォルダを移動した後で、ファイルがロックされ、上書きできないことです。私はまた、移動が終了したときにそれらを破棄することによって、ファイルを1つずつ移動しようとしました。
ファイルがブロックされていると私は
おかげ
private static void InitializePluginsFolder()
{
if (!Directory.Exists(Paths.PluginsPath))
{
Directory.CreateDirectory(Paths.PluginsPath);
}
// Find archive that contains plugins to deploy
var assembly = Assembly.GetExecutingAssembly();
if (assembly.Location == null)
{
throw new NullReferenceException("Executing assembly is null!");
}
var currentDirectory = new FileInfo(assembly.Location).DirectoryName;
if (currentDirectory == null)
{
throw new NullReferenceException("Current folder is null!");
}
// Check if previous installation contains a "Plugins" folder
var currentPluginsPath = Path.Combine(currentDirectory, "Plugins");
if (Directory.Exists(currentPluginsPath))
{
foreach (FileInfo fi in new DirectoryInfo(currentPluginsPath).GetFiles())
{
using (FileStream sourceStream = new FileStream(fi.FullName, FileMode.Open))
{
using (FileStream destStream = new FileStream(Path.Combine(Paths.PluginsPath, fi.Name), FileMode.Create))
{
destStream.Lock(0, sourceStream.Length);
sourceStream.CopyTo(destStream);
}
}
}
Directory.Delete(currentPluginsPath, true);
}
// Then updates plugins with latest version of plugins (zipped)
var pluginsZipFilePath = Path.Combine(currentDirectory, "Plugins.zip");
// Extract content of plugins archive to a temporary folder
var tempPath = string.Format("{0}_Temp", Paths.PluginsPath);
if (Directory.Exists(tempPath))
{
Directory.Delete(tempPath, true);
}
ZipFile.ExtractToDirectory(pluginsZipFilePath, tempPath);
// Moves all plugins to appropriate folder if version is greater
// to the version in place
foreach (var fi in new DirectoryInfo(tempPath).GetFiles())
{
if (fi.Extension.ToLower() != ".dll")
{
continue;
}
var targetFile = Path.Combine(Paths.PluginsPath, fi.Name);
if (File.Exists(targetFile))
{
if (fi.GetAssemblyVersion() > new FileInfo(targetFile).GetAssemblyVersion())
{
// If version to deploy is newer than current version
// Delete current version and copy the new one
// FAILS HERE
File.Copy(fi.FullName, targetFile, true);
}
}
else
{
File.Move(fi.FullName, targetFile);
}
}
// Delete temporary folder
Directory.Delete(tempPath, true);
}
コードを共有してください。 –
悲しいことに私たちは本当にそれから推測することはできません。 – BugFinder
申し訳ありませんコードをたくさん入れたくありませんでしたが、確かに役立つと思います。このメソッドは、Mainメソッドで呼び出された最初のメソッドです –