Reputation: 333
I'm running WiX 3.11 with a C# VSTO add-in. I need to create a feature that will install a folder, register it for sharing with everyone and remove the share upon uninstall.
I've tried 2 different approaches with no success (names and Ids are changed for simplification) :
<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>
With ShareFolderPermissions referenced in my new feature.
I've tried tweaking it a bit but it didn't work for me.
I used a non-english website to get set up on how to create a WiX CustomAction project to import into my Product.wxs file, and I'm giving below what helped me set up command lines in 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.cs in my CustomAction project (C#), logs are removed for clarity:
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;
}
I've tested my command in a separate application project with hard coded values I got from my msi logs.
msiexec /i "C:\MyPackage\Example.msi" /L*V "C:\log\example.log"
Code from the separate project (names changed on purpose) :
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());
}
The code from the separate project works fine as long as VS runs as administrator.
As of my msi, I've set Impersonate="no" so it asks for elevated rights on install / uninstall.
Both command are correctly called and end with no error. However, the targeted folder is never shared.
Can anybody help me out ?
Upvotes: 1
Views: 606
Reputation: 42126
What you describe - as far as I understand it - should be possible to achieve with built-in WiX constructs. Here are some pointers, I want to avoid too much code and markup duplication. Please DO follow link 2 at least. Link 1 is just for the record:
Always try to avoid custom actions (I know you state you need them).
Perhaps try this WiX sample for folder sharing?
Upvotes: 2