删除共享文件夹的一小段程序

这几天在局域网里面总是中毒,共享文件夹里面给人加入了许多熊猫,将共享文件夹删掉吧。写了个写程序完成这个功能

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Diagnostics;

namespace ShareKiller
{
    
/// <summary>
    
/// Form1 的摘要说明。
    
/// </summary>
    
/// 


    
    
public class Form1 : System.Windows.Forms.Form
    
{
        
private System.Windows.Forms.Button button1;
        
private System.Windows.Forms.ListView listView1;
        
private System.Windows.Forms.ColumnHeader columnHeader1;
        
private System.Windows.Forms.ColumnHeader columnHeader2;
        
private System.Windows.Forms.ColumnHeader columnHeader3;
        
/// <summary>
        
/// 必需的设计器变量。
        
/// </summary>

        private System.ComponentModel.Container components = null;

        
public Form1()
        
{
            
//
            
// Windows 窗体设计器支持所必需的
            
//
            InitializeComponent();

            
//
            
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
            
//
        }


        
/// <summary>
        
/// 清理所有正在使用的资源。
        
/// </summary>

        protected override void Dispose( bool disposing )
        
{
            
if( disposing )
            
{
                
if (components != null
                
{
                    components.Dispose();
                }

            }

            
base.Dispose( disposing );
        }


        
Windows 窗体设计器生成的代码

        
/// <summary>
        
/// 应用程序的主入口点。
        
/// </summary>

        [STAThread]
        
static void Main() 
        
{
            Application.Run(
new Form1());
        }

        

        
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;
    }

}

posted @ 2007-02-08 13:17  Robin Zhang  阅读(1461)  评论(1编辑  收藏  举报