Tuesday, 15 May 2012

Programmatically sharing a windows directory in C#


Sometime you are in a situation where you want to programmatically share a path and unshared it after use. This was a challenge I was faced with while working on an application that accepted user input, and then it would trigger a different process on another terminal.  The second application would then process and save the output on a path selected by the user. After going through the MSDN and a few google searches, here is what I developed

This article demonstrates how to share a directory and set NTFS permissions on it. start by importing the following library and namespaces.

using System.Security.Permissions;
using System.Security;
using System.Security.AccessControl;
using System.Security.Principal;


Create an instance of the group or user you want to share to, in my case here I want to share to everyone.

NTAccount ntAccount = new NTAccount("Everyone");

Get the group’s Security Identifier (SID).

SecurityIdentifier oGrpSID = (SecurityIdentifier)ntAccount.Translate(typeof(SecurityIdentifier));
byte[] utenteSIDArray = new byte[oGrpSID.BinaryLength];
oGrpSID.GetBinaryForm(utenteSIDArray, 0);

Create a trustee instance from the group SID above

ManagementObject oGrpTrustee = new ManagementClass(new ManagementPath("Win32_Trustee"), null);
oGrpTrustee["Name"] = "Everyone";
oGrpTrustee["SID"] = utenteSIDArray;

Create an Access Control Entry object. Give full access to the folder and allow sub folder to inherit

ManagementObject oGrpACE = new ManagementClass(new ManagementPath("Win32_Ace"), null);
oGrpACE["AccessMask"] = 2032127;//Full access
oGrpACE ["AceFlags"] = AceFlags.ObjectInherit | AceFlags.ContainerInherit; //propagate the AccessMask to the subfolders
oGrpACE["AceType"] = AceType.AccessAllowed;
oGrpACE["Trustee"] = oGrpTrustee;

Create a security descriptor; this will contain the security information for the group

ManagementObject oGrpSecurityDescriptor= new ManagementClass(new ManagementPath("Win32_SecurityDescriptor"), null);
oGrpSecurityDescriptor["ControlFlags"] = 4; //SE_DACL_PRESENT
oGrpSecurityDescriptor["DACL"] = new object[] { oGrpACE };

Lastly Share the directory in question and upgrade the security permissions. In my case is C:\testShare\, the Share Name is Test Share

string FolderPath=”c:\testshare\”;
string ShareName=”Test Share”;
string Description=”This save output”;
ManagementClass mc = new ManagementClass("win32_share");
ManagementBaseObject inParams = mc.GetMethodParameters("Create");
inParams["Description"] = Description;
inParams["Name"] = ShareName;
inParams["Path"] = FolderPath;
inParams["Type"] = 0x0; //Disk Drive
inParams["MaximumAllowed"] = null;
inParams["Password"] = null;
inParams["Access"] = oGrpSecurityDescriptor; 
ManagementBaseObject outParams = mc.InvokeMethod("Create", inParams, null);


Unshare the Directory
Create an Object to query the WMI Win32_Share API for shared files

ManagementObjectSearcher searcher = new ManagementObjectSearcher("select * from win32_share");
ManagementBaseObject outParams;
ManagementClass mc = new ManagementClass("Win32_Share"); //for local shares

Loop through all the local shares,

foreach (ManagementObject share in searcher.Get())
{
string type = share["Type"].ToString();

if (type == "0"// Check if it’s a DiskDrive                 {
string path = share["Name"].ToString(); //getting share path name

if (path == "testshare"){ //if it’s the testshare folder

outParams = share.InvokeMethod("delete"nullnull); //Delete the shares

if ((uint)(outParams.Properties["ReturnValue"].Value) != 0)
   {
      // unsuccessful do something

   }
else
   {
     // unsharing successful do something
   }
  }
 }
}


                                                                                

Friday, 4 May 2012

Creating a transform MST file


A while back a customer had a deployment problem where he wanted to install specific packages from our software. The software is usually shipped in an MSI which contains several packages in it.  Since the customer deploys the software through GPO, it wasn't possible to select the applications he wanted installed because the process is unattended.

To solve the problem I offered the customer a Transform file(.MST) and instructions how to use it.

What is a Transform?
Assuming you don't want to install the default software installation, a transform file or MST allows you to install selected features which the MSI advertises.

How can I create a Transform?
Some of the tools you can use to create a transform file include:Wise for Windows Installer, InstallShield Admin Studio and Orca.

In our case we will use Orca which can be downloaded free online.  Here is Microsoft Knowledge base article that can help you install orca http://support.microsoft.com/kb/255905

1.     launch Orca, Click on the file menu and open the MSI file you want to Edit. My example here use Visual Studio.net  2003 setup.



2.     From the tables Select ‘Feature’. This will present you with all the advertised features of the application




3.     On the Features table select the features to install and change the level column from 4 to 3. Since you also need to install the Parent feature, make a similar change too.                                                                                 -->In my  image below I want to install the all language tools therefore am going to change the parent feature level to 3 and apply the same to the sub_features.




4.     To change the Installation directory , select the Directory Table, Change the DefaultDir column for TARGETDIR to you specified path ie C:\vs_2003\



How Do I use the MST?
This is beyold the scope of this post but am going to explain the basics with the MSIEXEC command line and Group Policy Object.

On the command line type : MSIEXEC /i MySoftware.msi TRANSFORMS=MyTransform.mst


In Group Policy Object

1. Create a new Software Installation Package in the Computer Settings node of Group Policy Object Editor.
2. Select the MSI file, and then click Advanced.
3. On the Modifications tab, click Add and select the MST file you created.