http://www.cnblogs.com/jillzhang/archive/2007/03/01/660427.html
Code
public static ArrayList GetShareFiles()
{
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
p.Start();
string cmdText = "net share";
p.StandardInput.WriteLine(cmdText);
p.StandardInput.WriteLine("exit");
string strRst = p.StandardOutput.ReadToEnd();
p.Close();
ArrayList list = new ArrayList();
if(strRst.IndexOf("命令成功完成") != -1)
{
//成功
string ss = "-------------------------------------------------------------------------------";
int index1 = strRst.IndexOf(ss) + ss.Length;
ss = "命令成功完成";
int index2 = strRst.IndexOf(ss);
strRst = strRst.Substring(index1,index2 - index1);
string[] items = System.Text.RegularExpressions.Regex.Split(strRst,"\r\n",System.Text.RegularExpressions.RegexOptions.IgnoreCase);
foreach(string it in items)
{
if(it != "")
{
string[] sss = it.Split(' ');
int i = 0;
ShareInfo si = new ShareInfo();
foreach(string s in sss)
{
if(s != "")
{
if(i == 0)
{
si.Name = s;
}
else if(i == 1)
{
si.Resource = s;
}
else if(i == 2)
{
si.Note = s;
break;
}
i++;
}
}
list.Add(si);
}
}
}
return list;
}
public static void Excute(string cmdText)
{
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
p.Start();
p.StandardInput.WriteLine(cmdText);
p.StandardInput.WriteLine("exit");
while(!p.HasExited)
{
p.WaitForExit();
}
p.Close();
}
void BindData()
{
ArrayList list = GetShareFiles();
listView1.Items.Clear();
foreach(ShareInfo si in list)
{
ListViewItem item = new ListViewItem(new string[] { si.Name,si.Resource,si.Note });
listView1.Items.Add(item);
}
}
private void Form1_Load(object sender,System.EventArgs e)
{
BindData();
}
private void button1_Click(object sender,System.EventArgs e)
{
foreach(ListViewItem item in listView1.Items)
{
if(item.Selected)
{
string cmdText = "net share " + item.Text + " /delete";
Excute(cmdText);
}
}
BindData();
}
}
public class ShareInfo
{
public string Name;
public string Resource;
public string Note;
}
public static ArrayList GetShareFiles()
{
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
p.Start();
string cmdText = "net share";
p.StandardInput.WriteLine(cmdText);
p.StandardInput.WriteLine("exit");
string strRst = p.StandardOutput.ReadToEnd();
p.Close();
ArrayList list = new ArrayList();
if(strRst.IndexOf("命令成功完成") != -1)
{
//成功
string ss = "-------------------------------------------------------------------------------";
int index1 = strRst.IndexOf(ss) + ss.Length;
ss = "命令成功完成";
int index2 = strRst.IndexOf(ss);
strRst = strRst.Substring(index1,index2 - index1);
string[] items = System.Text.RegularExpressions.Regex.Split(strRst,"\r\n",System.Text.RegularExpressions.RegexOptions.IgnoreCase);
foreach(string it in items)
{
if(it != "")
{
string[] sss = it.Split(' ');
int i = 0;
ShareInfo si = new ShareInfo();
foreach(string s in sss)
{
if(s != "")
{
if(i == 0)
{
si.Name = s;
}
else if(i == 1)
{
si.Resource = s;
}
else if(i == 2)
{
si.Note = s;
break;
}
i++;
}
}
list.Add(si);
}
}
}
return list;
}
public static void Excute(string cmdText)
{
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
p.Start();
p.StandardInput.WriteLine(cmdText);
p.StandardInput.WriteLine("exit");
while(!p.HasExited)
{
p.WaitForExit();
}
p.Close();
}
void BindData()
{
ArrayList list = GetShareFiles();
listView1.Items.Clear();
foreach(ShareInfo si in list)
{
ListViewItem item = new ListViewItem(new string[] { si.Name,si.Resource,si.Note });
listView1.Items.Add(item);
}
}
private void Form1_Load(object sender,System.EventArgs e)
{
BindData();
}
private void button1_Click(object sender,System.EventArgs e)
{
foreach(ListViewItem item in listView1.Items)
{
if(item.Selected)
{
string cmdText = "net share " + item.Text + " /delete";
Excute(cmdText);
}
}
BindData();
}
}
public class ShareInfo
{
public string Name;
public string Resource;
public string Note;
}
通过多线程调用cmd执行net share,net share * /delete命令实现。不错。
C:"Windows"System32"Rundll32.exe ntlanui.dll,ShareManage 在运行中输入此命令。更方便。嘿嘿