WMIを使用してWindows共有メカニズムを操作する
ここでは、WMIを使用して共有ディレクトリが存在するかどうか、どのように認証を確立するか、どのように認証を切断するか、どのようにリモートで共有ディレクトリを確立するか、共有ディレクトリを削除するかについて説明します.
コードは次のとおりです.
転載する場合は、CSDN TJVictorコラムからオリジナルを明記してください.http://blog.csdn.net/tjvictor
コードは次のとおりです.
using System;
using System.Collections.Generic;
using System.Text;
using System.Management;
using System.Diagnostics;
using System.Threading;
namespace TJVictor.WMI
{
public class Win32_Share:WMIBaseClass
{
#region Property
private int timeout = 30;
public int TimeOut
{
get { return timeout; }
set { timeout = value; }
}
string wqlSelect = string.Empty;
#endregion
#region Construction
public Win32_Share()
: base()
{
wqlSelect = "select * FROM Win32_Share where Name='{0}'";
base.Connection();
}
public Win32_Share(string domain, string Ip, string user, string psd)
: base(domain, Ip, user, psd)
{
wqlSelect = "select * FROM Win32_Share where Name='{0}'";
base.Connection();
}
#endregion
#region public function
public bool IsShareDirectoryExist(string shareName)
{
if (!GetSelectQueryCollection(wqlSelect, shareName).Count.Equals(0))
return true;
else
return false;
}
public void ConnectionCredit()
{
string creditUser = base.User;
//
if (!base.Domain.Equals(string.Empty))
creditUser = base.Domain + "//" + base.User;
Process connectionCreditProcess = new Process();
connectionCreditProcess.StartInfo.CreateNoWindow = true;
connectionCreditProcess.StartInfo.UseShellExecute = false;
connectionCreditProcess.StartInfo.FileName = string.Format(@"C:/WINDOWS/system32/cmd.exe");
connectionCreditProcess.StartInfo.Arguments = string.Format("/c net use ////{0} /"{1}/" /user:/"{2}/"", base.Ip
, base.Password, creditUser);
connectionCreditProcess.Start();
int tempTimeout = this.timeout;
while (!connectionCreditProcess.HasExited)
{
Thread.Sleep(500);
connectionCreditProcess.Refresh();
if(tempTimeout.Equals(0))
throw new TJVictor.WMI.WmiException.ShareException(
string.Format(" {0} , ",base.Ip));
tempTimeout--;
}
}
public void DisconnectionCredit()
{
Process disconnectionCreditProcess = new Process();
disconnectionCreditProcess.StartInfo.CreateNoWindow = true;
disconnectionCreditProcess.StartInfo.UseShellExecute = false;
disconnectionCreditProcess.StartInfo.FileName = string.Format(@"C:/WINDOWS/system32/cmd.exe");
disconnectionCreditProcess.StartInfo.Arguments = string.Format(@"/c net use //{0}/ipc$ /delete", base.Ip);
disconnectionCreditProcess.Start();
int tempTimeout = this.timeout;
while (!disconnectionCreditProcess.HasExited)
{
Thread.Sleep(500);
disconnectionCreditProcess.Refresh();
if(tempTimeout.Equals(0))
throw new TJVictor.WMI.WmiException.ShareException(
string.Format(" {0} , ", base.Ip));
tempTimeout--;
}
}
public void CreateShareDir(string name, string shareDir)
{
Win32_Directory directory = new Win32_Directory(base.Domain, base.Ip, base.User, base.Password);
if(!directory.IsDirExist(name))
throw new TJVictor.WMI.WmiException.ShareException(
string.Format(" {0} {1} , ", base.Ip,shareDir));
ManagementClass processClass = new ManagementClass("Win32_Share");
processClass.Scope = base.Scope;
string method = "Create";
ManagementBaseObject inputArgs = processClass.GetMethodParameters(method);
inputArgs["Name"] = name;
inputArgs["Path"] = shareDir;
inputArgs["Description"] = "wmi Create shared";
inputArgs["Type"] = 0; // Disk share type
ManagementBaseObject result = processClass.InvokeMethod(method, inputArgs, null);
CheckExceptionClass.CheckShareException(int.Parse(result["ReturnValue"].ToString()));
//
int tempTimeout = this.timeout;
while (!IsShareDirectoryExist(name))
{
processClass.InvokeMethod(method, inputArgs, null);//
if(tempTimeout.Equals(0))
throw new TJVictor.WMI.WmiException.ShareException(
string.Format(" {0} {1} , ", base.Ip, name));
tempTimeout--;
}
}
public void DeleteShareDir(string name)
{
object result = 0;
foreach (ManagementObject mo in GetSelectQueryCollection(wqlSelect,name))
{
result = mo.InvokeMethod("Delete", null);
break;
}
CheckExceptionClass.CheckShareException(int.Parse(result.ToString()));
//
int tempTimeout = this.timeout;
while (IsShareDirectoryExist(name))
{
foreach (ManagementObject mo in GetSelectQueryCollection(wqlSelect, name))//
{
result = mo.InvokeMethod("Delete", null);
break;
}
if(tempTimeout.Equals(0))
throw new TJVictor.WMI.WmiException.ShareException(
string.Format(" {0} {1} , ", base.Ip, name));
tempTimeout--;
}
}
#endregion
}
}
転載する場合は、CSDN TJVictorコラムからオリジナルを明記してください.http://blog.csdn.net/tjvictor