windows 使用 C# 创建共享文件夹
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4337220/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Creating a shared folder using C#
提问by Scott
I've spent the last few days scouring the site looking for an answer to this problem, i need to create a folder and then make it a shared networked folder, I've tried several different sample code snippets from different sites
过去几天我一直在搜索站点以寻找此问题的答案,我需要创建一个文件夹,然后将其设为共享网络文件夹,我尝试了来自不同站点的几个不同示例代码片段
http://www.sarampalis.org/articles/dotnet/dotnet0002.shtml(this link is dead)
http://www.sarampalis.org/articles/dotnet/dotnet0002.shtml(此链接已失效)
but none seem to allow the folder to be shared
但似乎没有人允许共享文件夹
If anyone could be of help it'd be much aprriciated
如果有人可以提供帮助,那就太贵了
采纳答案by kashyapa
I did follow the link that you have in your question.
我确实按照您在问题中提供的链接进行操作。
I have a Win 7 Professional + VS.NET 2010 Professional as my dev environment on my laptop.
我的笔记本电脑上有一个 Win 7 Professional + VS.NET 2010 Professional 作为我的开发环境。
I took the code from the article and quickly fired up a devenv and executed the code. The code executed perfectly without any error. When i looked at the created directory you dont see a share icon on the folder but it is indeed shared. How can you check that: open a command prompt type "net share" and press enter you will see that it will list the folder c:\MyTestShare with a share name "My Test Share"
我从文章中取出代码并迅速启动了一个devenv并执行了代码。代码完美执行,没有任何错误。当我查看创建的目录时,您没有在文件夹上看到共享图标,但它确实是共享的。如何检查:打开命令提示符键入“net share”并按回车键,您将看到它将列出文件夹 c:\MyTestShare,共享名称为“My Test Share”
One more way to find out whether it is shared or not is to right click on the folder and look at the sharing tab. Check the Network Path label. It will clearly show the shared path.
确定它是否共享的另一种方法是右键单击文件夹并查看共享选项卡。检查网络路径标签。它将清楚地显示共享路径。
Hope this helps you. Lohith
希望这对你有帮助。洛希思
回答by Ran
If you don't find the real Windows API that does this, and you can settle for a dirty solution, you can do it by executing the command "net share".
如果您没有找到执行此操作的真正的 Windows API,并且您可以满足于一个肮脏的解决方案,则可以通过执行命令“net share”来实现。
For example, like this:
例如,像这样:
ProcessStartInfo info = new ProcessStartInfo("net", "share MyNewShare=c:\folder"); info.CreateNoWindow = true; Process.Start(info);
Note that in any case, in order to create a share you need administrative rights, so your code will have to run elevated if you're running on Win7/Vista with UAC enabled.
请注意,在任何情况下,为了创建共享,您都需要管理权限,因此如果您在启用了 UAC 的 Win7/Vista 上运行,则您的代码必须以提升的权限运行。