Jump to content
  • Advertisement
Sign in to follow this  
furiousp

C# - Removing a share from a directory

This topic is 4482 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I am using a ManagementClass in C# to execute the Create method in the Win32_Share class (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/win32_share.asp) , this works fine for sharing a directory. However I don't quite understand the Delete method. You'll notice from the documentation that it takes no arguments, does anyone know how it works? Or does anyone know another way of removing a share from a directory? Thanks

Share this post


Link to post
Share on other sites
Advertisement
Furious,

To make the long story short, Delete is an instance method, which means you need to get your hands on the ManagementObject representing the share, and then invoke the delete method on that. Then the share that the management object represents will be killed. I threw together a quick (incomplete and inefficient) wrapper class for Win32_Share, which you may find below, along with a piece of testing code, which seems to work fine.


using System;
using System.Collections.Generic;
using System.Text;
using System.Management;

namespace SharingTest
{
class Win32Share
{
public enum MethodStatus: uint
{
Success = 0, //Success
AccessDenied = 2, //Access denied
UnknownFailure = 8, //Unknown failure
InvalidName = 9, //Invalid name
InvalidLevel = 10, //Invalid level
InvalidParameter = 21, //Invalid parameter
DuplicateShare = 22, //Duplicate share
RedirectedPath = 23, //Redirected path
UnknownDevice = 24, //Unknown device or directory
NetNameNotFound = 25 //Net name not found
}

public enum ShareType: uint
{
DiskDrive = 0x0, //Disk Drive
PrintQueue = 0x1, //Print Queue
Device = 0x2, //Device
IPC = 0x3, //IPC
DiskDriveAdmin = 0x80000000, //Disk Drive Admin
PrintQueueAdmin = 0x80000001, //Print Queue Admin
DeviceAdmin = 0x80000002, //Device Admin
IpcAdmin = 0x80000003 //IPC Admin
}

private ManagementObject mWinShareObject;

private Win32Share(ManagementObject obj) { mWinShareObject = obj; }

#region Wrap Win32_Share properties
public uint AccessMask
{
get { return Convert.ToUInt32(mWinShareObject["AccessMask"]); }
}

public bool AllowMaximum
{
get { return Convert.ToBoolean(mWinShareObject["AllowMaximum"]); }
}

public string Caption
{
get { return Convert.ToString(mWinShareObject["Caption"]); }
}

public string Description
{
get { return Convert.ToString(mWinShareObject["Description"]); }
}

public DateTime InstallDate
{
get { return Convert.ToDateTime(mWinShareObject["InstallDate"]); }
}

public uint MaximumAllowed
{
get { return Convert.ToUInt32(mWinShareObject["MaximumAllowed"]); }
}

public string Name
{
get { return Convert.ToString(mWinShareObject["Name"]); }
}

public string Path
{
get { return Convert.ToString(mWinShareObject["Path"]); }
}

public string Status
{
get { return Convert.ToString(mWinShareObject["Status"]); }
}

public ShareType Type
{
get { return (ShareType)Convert.ToUInt32(mWinShareObject["Type"]); }
}
#endregion

#region Wrap Methods
public MethodStatus Delete()
{
object result = mWinShareObject.InvokeMethod("Delete", new object[] { });
uint r = Convert.ToUInt32(result);

return (MethodStatus)r;
}

public static MethodStatus Create(string path, string name, ShareType type, uint maximumAllowed, string description, string password)
{
ManagementClass mc = new ManagementClass("Win32_Share");
object[] parameters = new object[] { path, name, (uint)type, maximumAllowed, description, password, null };

object result = mc.InvokeMethod("Create", parameters);
uint r = Convert.ToUInt32(result);

return (MethodStatus)r;
}

// TODO: Implement here GetAccessMask and SetShareInfo similarly to the above
#endregion

public static IList<Win32Share> GetAllShares()
{
IList<Win32Share> result = new List<Win32Share>();
ManagementClass mc = new ManagementClass("Win32_Share");
ManagementObjectCollection moc = mc.GetInstances();

foreach (ManagementObject mo in moc)
{
Win32Share share = new Win32Share(mo);
result.Add(share);
}

return result;
}

public static Win32Share GetNamedShare(string name)
{
// Not a very efficient implementation obviously, but heck... This is sample code. ;)
IList<Win32Share> shares = GetAllShares();

foreach (Win32Share s in shares)
if (s.Name == name)
return s;

return null;
}
}

class Program
{
static void Main(string[] args)
{
// Create a share first:
Win32Share.MethodStatus result = Win32Share.Create("C:\\temp", "Temp", Win32Share.ShareType.DiskDrive, 10, "My Temp Folder", null);
if (result == Win32Share.MethodStatus.Success)
{
Console.WriteLine("Yay, we created a temp share!");
Win32Share tempShare = Win32Share.GetNamedShare("Temp");

Win32Share.MethodStatus deleteResult = tempShare.Delete();
if (deleteResult == Win32Share.MethodStatus.Success)
{
Console.WriteLine("Yay, we deleted temp share!");
}
}
}
}
}


Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!