最近的這個項目中有一要求就是要將數據放在局域網上共享,使其它機器來訪問。這樣就必須要將文件夾共享,本來是考慮用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.survivalescaperooms.com/wanlang/archive/2006/11/22/568705.html
新聞熱點
疑難解答