Working with Shared Folders
With Windows Home Server, shared content can be kept in managed shared folders on the home server. By using the API for Windows Home Server, you can write code that retrieves information about shared folders or that creates new shared folders with specific levels of permissions, which you can then use in your custom application.
Shared Folder Properties
An individual shared folder can be represented as a IShareInfo object, which has read-only properties for the following attributes:
Name
Text description
Folder Duplication status
Media Library Sharing status
Path
An individual shared folder can also be represented as a IShareInfo2 object, which has a read-only property for the globally unique identifier (GUID) attribute of the shared folder, in addition to the attributes of IShareInfo.
The IShareInfo2 object provides methods for setting and getting permissions on the shared folder.
Retrieve Information About Shared Folders
Step 1: Create an instance of WHSInfoClass
As with most API objects for Windows Home Server, retrieving information about shared folders begins by creating an instance of the WHSInfoClass, as follows:
WHSInfoClass pInfo = new WHSInfoClass();
Dim pInfo As New WHSInfoClass()
Step 2: Call the GetShareInfo() method
The GetShareInfo method is the WHSInfoClass method that you use to get information about shared folders on the home server. It returns an array of IShareInfo objects that represent all shared folders on the network:
Array shares = pInfo.GetShareInfo();
Dim shares As Array = pInfo.GetShareInfo()
Step 3: Loop through the array
Because the GetShareInfo method returns only an array of IShareInfo objects, you need to loop through the entire array to get information about a particular shared folder:
foreach (IShareInfo pShare in shares)
{
Console.WriteLine("Name: " + pShare.Name);
Console.WriteLine("Description: " + pShare.Description);
Console.WriteLine("Share Duplicated: " + pShare.IsDuplicated);
Console.WriteLine("Media Library: " + pShare.MediaConnectEnabled);
Console.WriteLine("Path To Share: " + pShare.Path);
}
Dim pShare As IShareInfo
For Each pShare In shares
Console.WriteLine("Name: " + pShare.Name)
Console.WriteLine("Description: " + pShare.Description)
Console.WriteLine("Share Duplicated: " + pShare.IsDuplicated)
Console.WriteLine("Media Library: " + pShare.MediaConnectEnabled)
Console.WriteLine("Path To Share: " + pShare.Path)
Next pShare
You can use this same approach whenever you are working with shared folders through the API for Windows Home Server.
Create a Shared Folder and Set Permissions
Step 1: Create an instance of WHSInfoClass
To create a shared folder, you must create an instance of WHSInfoClass. You can use the following code to create the instance:
IWHSInfo2 info = new WHSInfoClass();
Dim info As IWHSInfo2 = New WHSInfoClass()
Step 2: Create the shared folder
Use the CreateShare method to create a shared folder on the home server. Use the following code:
IShareInfo2 share = info.CreateShare("SharedFolderName", "SharedFolderDescription", 0);
Dim share As IShareInfo2 = info.CreateShare("SharedFolderName", "SharedFolderDescription", 0)
Replace SharedFolderName with the name that you want to assign the new shared folder. Replace SharedFolderDescription with the description of the new shared folder.
Step 3: Create the permission structure
To set permissions for users on a shared folder, you must create an instance of WHSUserPermission and define the username and level of permission. For this example, one user is assigned read-only permission, and the other user is assigned read and write permission as follows:
WHSUserPermission perm1 = new WHSUserPermission();
perm1.userName = "User1";
perm1.permission = WHSSharePermissions.WHS_SHARE_READ_ONLY;
WHSUserPermission perm2 = new WHSUserPermission();
perm2.userName = "User2";
perm2.permission = WHSSharePermissions.WHS_SHARE_READ_WRITE;
Dim perm1 As WHSUserPermission = New WHSUserPermission()
perm1.userName = "User1"
perm1.permission = WHSSharePermissions.WHS_SHARE_READ_ONLY
Dim perm2 As WHSUserPermission = New WHSUserPermission()
perm2.userName = "User2"
perm2.permission = WHSSharePermissions.WHS_SHARE_READ_WRITE
Step 4: Assign the permissions to the shared folder
After you create the permission structure, you can use the following code to assign the permissions to the shared folder:
Array permsArray = Array.CreateInstance(typeof(WHSUserPermission), 2);
permsArray.SetValue(perm1, 0);
permsArray.SetValue(perm2, 1);
share.SetPermissions(permsArray);
Dim permsArray As System.Array = System.Array.CreateInstance(GetType(WHSUserPermission), 2)
permsArray.SetValue(perm1, 0)
permsArray.SetValue(perm2, 1)
share.SetPermissions(permsArray)
See Also
Reference
Concepts
Extending Windows Home Server
Working with WHSInfoClass
Working with Client Computers
Working with Application Folders
Working with Managed Volumes
Working with Hard Disks
Working with Notifications
Working with Backups