gpt4 book ai didi

c# - 如何共享远程文件夹?

转载 作者:太空宇宙 更新时间:2023-11-03 14:17:08 25 4
gpt4 key购买 nike

我正在开发一个 .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,但看起来 ReturnValue 是我得到的全部。

是否有更好的共享远程文件夹的不同方法?

最佳答案

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

我最终得到了使用 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 中指定名称和密码。

关于c# - 如何共享远程文件夹?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6332615/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com