C#中使用net share命令时要注意的问题

王朝c#·作者佚名  2008-05-30
窄屏简体版  字體: |||超大  

最近的这个项目中有一要求就是要将数据放在局域网上共享,使其它机器来访问。这样就必须要将文件夹共享,本来是考虑用API来实现共享,不过后发现太繁琐,所以采用了NET SHARE这条WINDOWS命令。

System.Diagnostics.Process p = new System.Diagnostics.Process();

p.StartInfo.FileName = "cmd";

p.StartInfo.Arguments = " /c net share " + shareName + "=" + sharePath;

p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;

p.StartInfo.CreateNoWindow = true;

p.StartInfo.UseShellExecute = false;

p.StartInfo.RedirectStandardOutput = true;

p.Start();

p.WaitForExit();

shareName:可以是任何有效的共享名。sharePath:是要共享的完整路径。

项目制作过程没有发现任何不对,但到了安装到program files目录下就出现问题了,不能正确的共享,排查发现是路径中有空格的原因。在网上找了一些文章,解决办法是加上引号即可:

p.StartInfo.Arguments = " /c net share " + shareName + "=\"" + sharePath + "\"";

http://www.cnblogs.com/wanlang/archive/2006/11/22/568705.html

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航