簡體   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