2011-06-13 91 views
2

我正在研究.NET类,它将用于管理我们的Active Directory帐户的工具。我们的每个帐户都有一个网络主目录,根据我们正在使用的帐户类型,它可以位于两台不同的服务器上。如何共享远程文件夹?

我可以创建和删除文件夹就好了,但我遇到了共享文件夹的麻烦。 I found some code here这似乎是我想要的,但它不适合我。我得到的返回值是2,但我不确定那是什么表示。

这不应该是文件权限问题,因为我以我自己的方式运行我的测试应用程序,并且我完全控制了要共享的文件夹(及其每个父文件夹)。

这里是代码的我(修改)版本:

char[] delim = { '\\' }; 
// folderPath is a string (UNC path) 
string[] drivePath = folderPath.Split(delim); 

// Create a ManagementClass object 
ManagementClass managementClass = new ManagementClass("Win32_Share"); 

// Create ManagementBaseObjects for in and out parameters 
ManagementBaseObject inParams = 
    managementClass.GetMethodParameters("Create"); 
ManagementBaseObject outParams; 

// Set the input parameters 
inParams["Description"] = ""; 
inParams["Name"] = drivePath[3]; 
inParams["Path"] = folderPath; 
inParams["Type"] = 0x0; // Disk Drive 

// Invoke the method on the ManagementClass object 
outParams = managementClass.InvokeMethod("Create", inParams, null); 

我试过输出等outParams,但它看起来像返回值是我得到的。

有没有不同的方式来共享远程文件夹,可以更好地工作?

回答

1

我会回答自己,以防别人发现这种情况。

我结束了使用PSEXEC和NET SHARE的极其unsexy答案会:

// Retrieve drive path from AD 
char[] delim = { '\\' }; 
string[] drivePath = userAD.HomeDirectory.Split(delim); 

// Configure startup properties of process 
ProcessStartInfo startInfo = new ProcessStartInfo(); 
startInfo.CreateNoWindow = true; 
startInfo.FileName = "C:\\Windows\\System32\\psexec.exe"; 

// Build arguments for folder on alpha or student 
startInfo.Arguments = "\\\\" + serverName + " -s net share " + shareName + "=" folderPath + "$ /GRANT:\"authenticated users\",full"; 

Process process = new Process(); 

process.StartInfo = startInfo; 
process.Start(); 
process.WaitForExit(); 

注意,在我们的环境中的程序就已经被谁拥有作为文件夹的完全控制权限的用户下运行共享。要允许类似于非特权用户,您必须在startInfo.Arguments中指定名称和密码。