Wix:如何安装共享文件夹

问题描述

我正在运行带有C#VSTO加载项的WiX 3.11。 我需要创建一个功能来安装文件夹,注册文件夹以便与所有人共享,并在卸载时删除该共享。

我尝试了2种不同的方法,但均未成功(为简化起见,更改了名称和ID):

  1. 借助下面的链接,我试图在创建文件夹时创建权限:
    WiX set App_Data folder permission to modify for NetworkService
    Wix: CustomAction to set Folder permissions
    Set permissions for existing folders and files in ProgramData with WiX Toolset
 <Directory Id="TARGETDIR" Name="SourceDir">    
   <Directory Id="WINDOWSVOLUME">
    <Directory Id="INSTALLFOLDER" Name=".">
     <Directory Id="FOLDERTOSHARE" Name="Share">
       <Component Id="ShareFolderPermissions" Guid="SOMEGUID">
         <CreateFolder>
           <util:PermissionEx User="Everyone" GenericAll="yes" />
         </CreateFolder>
       </Component>
     </Directory>
   </Directory>
 </Directory>

在我的新功能中引用了ShareFolderPermissions。
我尝试了一下调整,但对我来说不起作用。

  1. 我的第二个解决方案是尝试通过CustomActions使用Windows命令来完成工作(更糟:无论如何我以后都需要CustomActions)。
    在某个时候它运行良好,但是我无法实现整个过程。

我使用一个非英语网站来设置如何创建一个WiX CustomAction项目以导入到我的Product.wxs文件中,并在下面提供了帮助我在C#中设置命令行的内容
How to use Windows command with C#
Net share documentation

Product.wxs(xml):

<?if $(var.Configuration) = MyShareConfiguration?>
  <Property Id="SHAREFEATURE" Value="1" />
<?endif?>
  
<Feature Id="ShareFeature" Title="ShareSetup" Level="0">
  <Condition Level="1">SHAREFEATURE</Condition>
  <ComponentGroupRef Id="MyShareFiles"/>
</Feature>
  
<CustomAction Id="CustomActionShareFolder" BinaryKey="CustomActionProject" DllEntry="ShareFolder" Execute="immediate" Return="check" Impersonate="no" />
<CustomAction Id="CustomActionUnShareFolder" BinaryKey="CustomActionProject" DllEntry="UnShareFolder" Execute="immediate" Return="check" Impersonate="no" />

<Binary Id="CustomActionProject" SourceFile="CustomActionProjectPath" />

<InstallExecuteSequence>
  <Custom Action="CustomActionShareFolder" After="InstallFiles"> SHAREFEATURE AND (NOT REMOVE) </Custom>
  <Custom Action="CustomActinUnShareFolder" Before="RemoveFiles"> SHAREFEATURE AND (REMOVE~="ALL") </Custom>
</InstallExecuteSequence>

CustomAction项目(C#)中的CustomAction.cs,为了清楚起见,删除了日志:

private static readonly string SHAREFEATURENAME= "MyShareFeatureName";
private static void ExecuteCMD(string command) 
{
    System.Diagnostics.Process process = new System.Diagnostics.Process();
    System.Diagnostics.processstartinfo startInfo = new System.Diagnostics.processstartinfo
    {
        WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden,FileName = "CMD.exe",Arguments = command,CreateNowindow = true,UseShellExecute = true,Verb = "runas"
    };
    process.StartInfo = startInfo;
    process.Start(); 
}

[CustomAction] 
public static ActionResult ShareFolder(Session session)
{
    ActionResult result = ActionResult.Success;
    string directoryPath = session.GetTargetPath("FOLDERTOSHARE"); // See Directory setup in 1.
    string command = string.Format("/c net share {0}=\"{1}\"",SHAREFEATURENAME,directoryPath.Remove(directoryPath.Length - 1)); // Remove '\' at the end of the path

    try
    {
        ExecuteCMD(command);
    }     
    catch (Exception)
    {
        result = ActionResult.Failure;
    }

    return result; 
}

[CustomAction] 
public static ActionResult UnShareFolder(Session session) 
{
    ActionResult result = ActionResult.Success;
    string command = string.Format("/c net share {0} /delete",SHAREFEATURENAME);

    try
    {
        ExecuteCMD(command);
    }
    catch (Exception)
    {
        result = ActionResult.Failure;
    }

    return result; 
}

我已经在一个单独的应用程序项目中使用从msi日志中获得的硬编码值测试了命令。

msiexec /i "C:\MyPackage\Example.msi" /L*V "C:\log\example.log"

来自单独项目的代码(故意更改名称):

static void Main(string[] args)
{
    string command = "/c net share SHAREFEATURENAME=\"MyFolderPath\"";
    //string command = "/c net share SHAREFEATURENAME /delete";

     Console.WriteLine("Command : " + command);

     Process cmd = new Process();
     cmd.StartInfo.FileName = "cmd.exe";
     cmd.StartInfo.RedirectStandardInput = true;
     cmd.StartInfo.RedirectStandardOutput = true;
     cmd.StartInfo.CreateNowindow = true;
     cmd.StartInfo.UseShellExecute = false;
     cmd.StartInfo.Arguments = command;
     cmd.StartInfo.Verb = "runas";
     cmd.Start();

     cmd.StandardInput.WriteLine("echo test");
     cmd.StandardInput.Flush();
     cmd.StandardInput.Close();
     cmd.WaitForExit();
     Console.WriteLine(cmd.StandardOutput.ReadToEnd());
}

只要VS以管理员身份运行,来自单独项目的代码就可以正常工作。
从我的msi开始,我设置了Impersonate =“ no”,因此它要求在安装/卸载时提升权限。

两个命令都被正确调用并且没有错误结束。但是,目标文件夹永远不会共享。

有人可以帮我吗?

解决方法

据我所知,您所描述的内容应该可以通过内置的WiX构造来实现。这里有一些指针,我想避免过多的代码和标记重复。请至少遵循链接2。链接1仅用于记录:

  1. Always try to avoid custom actions(我知道您声明需要它们)。

  2. 也许尝试this WiX sample for folder sharing