close

1. 首先:要將「System.Management」加入參考。

2. 程式碼:

ManagementClass mClass = new ManagementClass( "Win32_Share" );
ManagementBaseObject mBaseObj_in = mClass.GetMethodParameters( "Create" );

mBaseObj_in["Path"] = installDirInfo.FullName ;  // 資料夾位置.
mBaseObj_in["Type"] = 0x0;  // 共用類型.

ManagementBaseObject mBaseOjb_out = mClass.InvokeMethod( "Create", mBaseObj_in, null );
if ( (uint)(mBaseOjb_out.Properties["ReturnValue"].Value) != 0)
{
    Console.WriteLine( "無法共享資料夾: " + mBaseOjb_out.Properties["ReturnValue"].Value );
}

/// ReturnValue:
///   0 – Success
///   2 – Access denied
///   8 – Unknown failure
///   9 – Invalid name
///  10 – Invalid level
///  21 – Invalid parameter
///  22 – Duplicate share
///  23 – Redirected path
///  24 – Unknown device or directory
///  25 – Net name not found

 

參考:
Visual C# 透過WMI實作資料夾共享功能 | .NET Factory - 點部落
c# - Creating share programmatically fails with error 9 - Stack Overflow

 

arrow
arrow
    文章標籤
    code C# share folder
    全站熱搜

    Robert 發表在 痞客邦 留言(1) 人氣()