我有一个 WIX 安装应用程序和许多源文件:
...
<Directory Id="dirF21F1AE09DCD1D651EFCA4E6AD334FAC" Name="myservice">
<Component Id="cmp503CB14E95C2C333DCE9E73FC1BB2E9A" Guid="{29FDDCA4-E70D-41AA-B1C8-06AD9A07810D}">
<File Id="fil2FE62A0172300DF74F1725E28B7FA003" KeyPath="yes" Source="$(var.SourcePath)\myservice\common_base.dll" />
</Component>
</Directory>
...
并且此文件由 ref 复制:
<ComponentGroupRef Id="InstallSources"/>
在复制文件之前,我需要在自定义操作中访问 common_base.dll。我想将它复制到临时文件夹进行一些操作:
private static void CopyCommonDll(Session session)
{
try
{
var dllPath = session["get path here"]; // or can I get dllPath the other way?
session.InfoLog("Dll path: {0}", dllPath);
var destPath = Path.Combine(Path.GetTempPath(), Path.GetFileName(dllPath));
session.InfoLog("destPath dll path: {0}", dllPath);
File.Copy(dllPath, destPath);
session.InfoLog("file copied!");
// some code here
File.Delete(destPath);
}
catch (Exception e)
{
session.ErrorLog(e);
}
}
我该怎么做?
最佳答案
解决方案是将您希望在自定义操作安装期间访问的文件作为嵌入式资源添加到自定义操作的项目中。在自定义操作中,您可以从程序集资源中获取它。
var assembly = Assembly.GetExecutingAssembly();
var resourceName = "CustomActions.Resources.common_base.dll";
byte[] bytes = null;
using (Stream stream = assembly.GetManifestResourceStream(resourceName))
{
if (stream != null)
{
session.InfoLog("dll found was in resources");
bytes = new byte[(int)stream.Length];
stream.Read(bytes, 0, (int)stream.Length);
}
}
if (bytes != null)
{
// save file here!
File.WriteAllBytes(destPath, bytes);
}
关于c# - WIX 如何从自定义操作访问源文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35743982/