我需要共享一个具有读写权限的文件夹。
在 Windows 7 中,我可以通过执行以下操作来共享它:
net share temp=A:\MySharedFolder /GRANT:Everyone,FULL
如果我在 windows xp 中执行相同的代码,它就不起作用。但是我可以执行:
净共享温度=C:\MySharedFolder
但该文件夹不会以完全权限共享。
编辑
JotaBe 发布了一个链接,我从该链接构建了以下似乎可以工作的程序。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
namespace ConsoleApplication1
{
class Program
{
[DllImport("Netapi32.dll")]
private static extern uint NetShareAdd(
[MarshalAs(UnmanagedType.LPWStr)] string strServer,
Int32 dwLevel,
ref SHARE_INFO_502 buf,
out uint parm_err
);
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_TEMPORARY = 0x40000000,
STYPE_SPECIAL = 0x80000000,
}
[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_remark;
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;
}
static void Main(string[] args)
{
string shareName = "testshare";
string shareDesc = "This is a test share kekelar2000";
string path = @"C:\MyShareDirectory"; // 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_remark = shareDesc;
info.shi502_permissions = 0;
info.shi502_max_uses = -1;
info.shi502_current_uses = 0;
info.shi502_path = path;
info.shi502_passwd = null;
info.shi502_reserved = 0;
info.shi502_security_descriptor = IntPtr.Zero;
uint error = 0;
uint result = NetShareAdd("192.168.0.120", 502, ref info, out error);
}
}
}