你問這個問題前一段時間,你已經接受了答案,但我仍然會嘗試以另一種方式。我有同樣的問題,這是我如何解決它:
我將dll作爲一個資源添加到我的C#項目,並將此代碼添加到我的主要方法(啓動您的主要winform的那個)。
public static void Main(string[] args)
{
if (InitdeDEDll()) // Create dll if it's missing.
{
// Restart the application if the language-package was added
Application.Restart();
return;
}
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new YOURMAINFORM());
}
private static bool InitdeDEDll() // Initialize the German de-DE DLL
{
try
{
// Language of my package. This will be the name of the subfolder.
string language = "de-DE";
return TryCreateFileFromRessource(language, @"NAMEOFYOURDLL.dll",
NAMESPACEOFYOURRESSOURCE.NAMEOFYOURDLLINRESSOURCEFILE);
}
catch (Exception)
{
return false;
}
}
private static bool TryCreateFileFromRessource(string subfolder, string fileName, byte[] buffer)
{
try
{
// path of the subfolder
string subfolderPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + (subfolder != "" ? @"\" : "") + subfolder;
// Create subfolder if it doesn't exist
if (!Directory.Exists(subfolder))
Directory.CreateDirectory(subfolderPath);
fileName = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + @"\" + subfolder + (subfolder!=""[email protected]"\":"") + fileName;
if (!File.Exists(fileName)) // if the dll doesn't already exist, it has to be created
{
// Write dll
Stream stream = File.Create(fileName);
stream.Write(buffer, 0, buffer.GetLength(0));
stream.Close();
}
else
{
return false;
}
}
catch
{
return false;
}
return true;
}
}
注意:這將重新創建該文件夾和語言的DLL,如果它丟失,所以你不必關心嗎,你將該文件夾複製,並與您的exe文件的DLL 。如果你希望它完全消失,當然這不是正確的做法。
http://stackoverflow.com/questions/1453755/how-to-embed-a-satellite-assembly-into-the-exe-file –
我試圖ILMerge已經張貼在此之前。試過Assembly Linker,但無法使其工作。由於引用csc.exe給出了一些錯誤,似乎彙編鏈接器導致可執行文件不使用資源DLL。 – PythEch
我不明白MS爲什麼要重新發明輪子... – PythEch