我在我的WPF程序中使用了一個通用實用dll文件。我的程序做的第一件事是檢查一個更新的dll文件並將其複製到正在執行的目錄 - 所有這些都不需要引用dll中的任何方法或屬性。DLL加載時間在Visual Studio與EXE文件中有所不同
當我從Visual Studio(v10)編譯並運行程序時,所有內容都按預期工作。該程序啓動,檢查dll文件,如果需要複製,然後繼續使用程序集。
如果我從Windows資源管理器運行編譯後的.exe文件,它看起來第一件事就是加載Util.dll程序集。這將鎖定文件,並且不允許我更新它。
有沒有人有一些洞察力,爲什麼程序會在Visual Studio vs .exe文件中有不同的運行方式?在追蹤運行.exe文件時導致程序集加載的任何想法?
下面是程序啓動時的代碼片段:
void AppLoad(object sender, StartupEventArgs e)
{
//Used to see what assemblies are loaded.
System.Text.StringBuilder sb = new System.Text.StringBuilder();
foreach (var item in AppDomain.CurrentDomain.GetAssemblies())
{
sb.AppendLine(item.FullName.ToString());
}
System.IO.File.WriteAllText(AppDomain.CurrentDomain.BaseDirectory + "test.txt", sb.ToString());
//Check for the latest Util dll.
if (!UpdateUtil())
{
//Shutdown.
Application.Current.Shutdown();
return;
}
//Start the main window.
MainWindow m = new MainWindow();
m.Show();
}
bool UpdateUtil()
{
//Verify network path.
if (!Directory.Exists(_componentPath))
{
MessageBox.Show("The program encountered an error.\r\rPlease contact your Program Administrator with the following information:\r\r" +
"Program Name - Genesis Admin\r\r" +
"Error Message - Network Component path not found.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
return false;
}
//Verify file existance.
string sourceFileName = _componentPath + "Util.dll";
if (!File.Exists(sourceFileName))
{
MessageBox.Show("The program encountered an error.\r\rPlease contact your Program Administrator with the following information:\r\r" +
"Program Name - Genesis Admin\r\r" +
"Error Message - Network Util file not found.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
return false;
}
string destFileName = AppDomain.CurrentDomain.BaseDirectory + "Util.dll";
if (!File.Exists(destFileName) || File.GetLastWriteTime(sourceFileName) > File.GetLastWriteTime(destFileName))
File.Copy(sourceFileName, destFileName, true);
return true;
}
僅供參考因爲大多數IT管理員會忽視這種做法(非常不安全),所以這在產品環境中將無法使用。 – tsells