繁体   English   中英

如何防止 C# 将临时路径作为原始路径的前缀?

[英]How do I prevent C# from prefixing the temporary path to my original path?

我正在尝试在自定义操作中编辑已安装的cmd文件:

<CustomAction
 Id="CA_EditScriptAction"
 Return="check"
 Execute="deferred"
 Impersonate="no"
 BinaryKey="CustomActions"
 DllEntry="EditScriptAction"
 />

它在PublishProduct操作之后调用。 问题是,我找不到该文件,因为由于某种原因,当它使用诸如Directory.GetFiles(ABSOLUTE_PATH)之类的方法时,它会在绝对路径之前添加一个临时安装程序目录的路径。 以下是日志:

SFXCA: Extracting custom action to temporary directory: C:\Windows\Installer\MSI941A.tmp-\
SFXCA: Binding to CLR version v4.0.30319
Calling custom action MSIActions!MSIActions.CustomActions.EditScriptAction
CUSTOM_DEBUG: C:\Program Files\SomeApp\
ERROR in EditScriptAction: System.IO.DirectoryNotFoundException: Could not find a part of the path 'C:\Windows\Installer\MSI941A.tmp-\C:\Program Files\SomeApp\'.
   at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   at System.IO.FileSystemEnumerableIterator`1.CommonInit()
   at System.IO.FileSystemEnumerableIterator`1..ctor(String path, String originalUserPath, String searchPattern, SearchOption searchOption, SearchResultHandler`1 resultHandler, Boolean checkHost)
   at System.IO.Directory.GetFiles(String path)
   at MSIActions.CustomActions.EditScriptAction(Session session) in D:\Projects\Visual Studio\app\MSIActions\CustomAction.cs:line 57
CustomAction CA_EditScriptAction returned actual error code 1603 (note this may not be 100% accurate if translation happened inside sandbox)

正如我在日志中看到的那样,它将操作提取到C:\Windows\Installer\MSI941A.tmp-\文件夹中,而不是 C# 方法将该文件夹添加到我传递给函数的任何路径中,如下所示:

ERROR in EditScriptAction: System.IO.DirectoryNotFoundException: Could not find a part of the path 'C:\Windows\Installer\MSI941A.tmp-\C:\Program Files\SomeApp\'.

而我通过的路径是C:\Program Files\SomeApp\

这是它发生的代码:

 [CustomAction]
 public static ActionResult EditWebsharkScriptAction(Session session)
 {            
     CustomActionData data = session.CustomActionData;
     var install_dir = data["INSTALLFOLDER"];
     session.Log("CUSTOM_DEBUG: " + install_dir);

     var files = Directory.GetFiles(install_dir); //Exception is thrown here

     //the rest of the code
 }

如何防止 C# 将临时路径作为原始路径的前缀?

编辑:正如我所观察到的,这只发生在非常量变量上。 如果我使用硬编码路径创建一个 const 变量,则 C# 不会预先添加该临时文件夹路径...

延迟的自定义操作只能在执行序列表中的 InstallInitialize 和 InstallFinalize 操作之间进行排序。

所以你有两个选择:

  1. 在 InstallInitialize 和 InstallFinalize 之间移动执行操作。

  2. 将执行更改为“立即”。 在某些情况下,不推荐这种方式,一切都取决于您的目的。

     <CustomAction Id="CA_EditScriptAction" Return="check" Execute="immediate" Impersonate="no" BinaryKey="CustomActions" DllEntry="EditScriptAction" />

并将您的 CustomAction 更改为

[CustomAction]
public static ActionResult EditWebsharkScriptAction(Session session)
{            
    var install_dir = session["INSTALLFOLDER"];
    session.Log("CUSTOM_DEBUG: " + install_dir);
    var files = Directory.GetFiles(install_dir); 
}

问题在于方式,我正在将属性传递给我的自定义操作。

我曾经通过 Property 元素来做到这一点,但在延迟自定义操作的情况下,我必须通过另一个 CustomAction 元素传递它们,如本答案中所示。

不幸的是,这仅适用于即时 CA,这意味着如果您想在延迟 CA 中使用此属性的值,您将需要有两个自定义操作:

  1. CA 1 为立即执行的 CA 设置 CustomActionData。 (请记住使用为您的 CustomAction 定义的相同名称命名属性。
  2. CA 2 具有使用 CustomActionData 的特定逻辑的 CA。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM