如何共享远程文件夹?

发布于 2024-11-15 05:32:23 字数 1160 浏览 2 评论 0原文

我正在开发一个 .NET 类,该类将在管理我们的 Active Directory 帐户的工具中使用。我们的每个帐户都有一个网络主目录,该目录可以位于几个不同的服务器上,具体取决于我们使用的帐户类型。

我可以很好地创建和删除文件夹,但在共享文件夹时遇到问题。 我在这里找到了一些代码,这似乎就是我想要的,但它对我来说不能正常工作。我得到的返回值是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,但看起来 ReturnValue 就是我得到的全部。

是否有其他更好的方式来共享远程文件夹?

I'm working on a .NET class that will be used in tools to manage our Active Directory accounts. Each of our accounts gets a network home directory, which can be located on a couple different servers, depending on what type of account we're working with.

I can create and delete the folders just fine, but I'm running into trouble sharing the folders. I found some code here that seems to be what I want, but it isn't working properly for me. The return value I get is 2, but I'm not sure what that indicates.

This shouldn't be a file permission issue, since I'm running my test application as myself, and I have full control of the folder that I'm trying to share (and each of its parent folders).

Here's my (modified) version of the code:

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);

I've tried outputting the other outParams, but it looks like ReturnValue is all I get.

Is there a different way to share a remote folder that would work better?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

毁我热情 2024-11-22 05:32:23

我会自己回答,以防其他人稍后发现。

我最终采用了使用 PSExec 和 NET SHARE 的极其不性感的答案:

// 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 中指定名称和密码。

I'll answer myself, in case someone else finds this later.

I ended up going with the extremely unsexy answer of using PSExec and NET SHARE:

// 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();

Note that in our environment the program will already be running under a user who has Full Control permissions on the folder being shared. To allow similar for a non-privileged user, you have to specify a name and password in startInfo.Arguments.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文