想实现的功能是:
用代码设置本地文件夹为共享文件夹。要求可以设置访问权限控制(比如为只读共享还是完全共享)。另外,如何取消该共享?
请各位大虾帮忙解答!另:看到网上介绍的方法好像有两种:一种使用DOS命令,一种调用API:netShareADD()。还有就是用WMI(我的理解似乎还是调用API,而且没有不知道如何使用才能实现以上功能)。可是我想知道在c#或者.net framework里有没有简单的方法并且实现以上功能的?
用代码设置本地文件夹为共享文件夹。要求可以设置访问权限控制(比如为只读共享还是完全共享)。另外,如何取消该共享?
请各位大虾帮忙解答!另:看到网上介绍的方法好像有两种:一种使用DOS命令,一种调用API:netShareADD()。还有就是用WMI(我的理解似乎还是调用API,而且没有不知道如何使用才能实现以上功能)。可是我想知道在c#或者.net framework里有没有简单的方法并且实现以上功能的?
using System;
using System.Management;
using System.Windows.Forms;namespace WMISample
{
public class CallWMIMethod
{
public static void Main()
{
try
{
ManagementClass classInstance =
new ManagementClass("root\\CIMV2", "Win32_Share", null); // Obtain in-parameters for the method
ManagementBaseObject inParams =
classInstance.GetMethodParameters("Create"); // Add the input parameters.
inParams["Name"] = "share name";
inParams["Path"] = "D:\\新建文件夹";
inParams["Type"] = 0; // Execute the method and obtain the return values.
ManagementBaseObject outParams =
classInstance.InvokeMethod("Create", inParams, null); // List outParams
Console.WriteLine("Out parameters:");
Console.WriteLine("ReturnValue: " + outParams["ReturnValue"]);
}
catch(ManagementException err)
{
MessageBox.Show("An error occurred while trying to execute the WMI method: " + err.Message);
}
}
}
}
你可以自己看看Win32_SecurityDescriptor
上面的代码是Create,当我调用Delete的时候就不知道怎么做了,因为Delete没有参数,可是我传进去空又报错,纳闷。
总之,先谢谢你的回答。继续等待好的解决方法
public class ShareAPI
{
private enum NetError : uint
{
NERR_Success = 0,
NERR_BASE = 2100,
NERR_UnknownDevDir = (NERR_BASE + 16),
NERR_DuplicateShare = (NERR_BASE + 18),
NERR_BufTooSmall = (NERR_BASE + 23),
} private enum SHARE_TYPE : uint
{
STYPE_DISKTREE = 0,
STYPE_PRINTQ = 1,
STYPE_DEVICE = 2,
STYPE_IPC = 3,
STYPE_SPECIAL = 0x80000000,
} private enum SHARE_PERMISSION : uint
{
ACCESS_READ = 1,
ACCESS_WRITE = 2,
} [StructLayout(LayoutKind.Sequential)]
private struct SHARE_INFO_502
{
[MarshalAs(UnmanagedType.LPWStr)]
public string shi502_netname;
public SHARE_TYPE shi502_type;
[MarshalAs(UnmanagedType.LPWStr)]
public string shi502_re;
public Int32 shi502_permissions;
public Int32 shi502_max_uses;
public Int32 shi502_current_uses;
[MarshalAs(UnmanagedType.LPWStr)]
public string shi502_path;
[MarshalAs(UnmanagedType.LPWStr)]
public string shi502_passwd;
public Int32 shi502_reserved;
public IntPtr shi502_security_descriptor;
} [DllImport("Netapi32.dll")]
private static extern int NetShareAdd(
[MarshalAs(UnmanagedType.LPWStr)] string strServer,
Int32 dwLevel,
ref SHARE_INFO_502 buf,
out uint parm_err); [DllImport("Netapi32.dll")]
private static extern int NetShareDel(
[MarshalAs(UnmanagedType.LPWStr)] string strServer,
[MarshalAs(UnmanagedType.LPWStr)] string netName,
Int32 reserved); public static int Test()
{
string shareName = "temp";
string shareDesc = "just test";
string path = @"C:\temp"; // do not append comma, it'll fail SHARE_INFO_502 info = new SHARE_INFO_502();
info.shi502_netname = shareName;
info.shi502_type = SHARE_TYPE.STYPE_DISKTREE;
info.shi502_re = shareDesc;
info.shi502_permissions = 0; // ignored for user-level security
info.shi502_max_uses = -1;
info.shi502_current_uses = 0; // ignored for set
info.shi502_path = path;
info.shi502_passwd = null; // ignored for user-level security
info.shi502_reserved = 0;
info.shi502_security_descriptor = IntPtr.Zero; uint error = 0;
//return NetShareAdd(null, 502, ref info, out error); Int32 reserved = 0; return NetShareDel(null, "temp", reserved);
}
};测试通过,可是设置共享权限不成功,不知道为什么?上面的info.shi502_permissions = 0;好像不起作用,有哪位高手能给解释一下吗?
using System;
using System.Management;
using System.Windows.Forms;namespace WMISample
{
public class CallWMIMethod
{
public static void Main()
{
try
{
ManagementObject classInstance =
new ManagementObject("root\\CIMV2",
"Win32_Share.Name='Temp'",
null); // no method in-parameters to define
// Execute the method and obtain the return values.
ManagementBaseObject outParams =
classInstance.InvokeMethod("Delete", null, null); // List outParams
Console.WriteLine("Out parameters:");
Console.WriteLine("ReturnValue: " + outParams["ReturnValue"]);
}
catch(ManagementException err)
{
MessageBox.Show("An error occurred while trying to execute the WMI method: " + err.Message);
}
}
}
}