如何共享远程文件夹?
我正在开发一个 .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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我会自己回答,以防其他人稍后发现。
我最终采用了使用 PSExec 和 NET SHARE 的极其不性感的答案:
请注意,在我们的环境中,该程序已经在对共享文件夹具有完全控制权限的用户下运行。要允许非特权用户执行类似操作,您必须在
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:
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
.