读书人

C#设立共享文件夹权限让所有人都可以

发布时间: 2013-04-26 16:27:53 作者: rapoo

C#设置共享文件夹权限,让所有人都可以读写
网上找了相关的代码测试都不行,不论是以普通用户允许还是以管理员账户允许。

麻烦大家帮帮忙。
目前测试的代码


public static void SetFileRole()
{

DirectorySecurity fSec = new DirectorySecurity();

fSec.AddAccessRule(new FileSystemAccessRule(@"Everyone", FileSystemRights.FullControl, AccessControlType.Allow));

System.IO.Directory.SetAccessControl(@"E:\sharefile2", fSec);
}






[解决办法]
你的代码操作的是操作权限,而不是共享权限。

要设置共享权限,可以使用NetShareSetInfo API,使用SHARE_INFO_2结构。
[解决办法]
在网上查查如何调用API,应该很好实现的
[解决办法]
http://zhidao.baidu.com/question/357548900.html
[解决办法]
http://download.csdn.net/download/pojianbing/203745
这里有个共享程序,检查可用
[解决办法]
/*
* Created by SharpDevelop.
* User: Administrator
* Date: 2007/07/03
* Time: 14:02
*
* To change this template use Tools
[解决办法]
Options
[解决办法]
Coding
[解决办法]
Edit Standard Headers.
*/

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.Management;

namespace Share
{
/// <summary>
/// Description of MainForm.
/// </summary>
public partial class MainForm
{
[STAThread]
public static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
}

public MainForm()
{
//
// The InitializeComponent() call is required for Windows Forms designer support.
//
InitializeComponent();

//
// TODO: Add constructor code after the InitializeComponent() call.
//
}

void Button1Click(object sender, System.EventArgs e)
{
shareFolder("d:\\","D","Description");
}

private void shareFolder(string FolderPath, string ShareName, string Description)
{// 创建共享目录
try
{
ManagementClass managementClass = new ManagementClass("Win32_Share");


ManagementBaseObject inParams = managementClass.GetMethodParameters("Create");
ManagementBaseObject outParams;
inParams["Description"] = Description;
inParams["Name"] = ShareName;
inParams["Path"] = FolderPath;
inParams["Type"] = 0x0;
outParams = managementClass.InvokeMethod("Create", inParams, null);

if ((uint)(outParams.Properties["ReturnValue"].Value) != 0)
{
throw new Exception("Unable to share directory.");
}
}
catch
{
return ;
}
}
}
}


[解决办法]
没实现过,不过感觉不行吧,要是可以用程序随意改文件夹的权限不就是木马了吗?不明白!

读书人网 >C#

热点推荐