zoukankan      html  css  js  c++  java
  • 程序的自我删除

    转网上的:

    [DllImport("kernel32.dll")] public static extern uint WinExec(string lpCmdLine, uint uCmdShow);

     

    private void button1_Click(object sender, EventArgs e) {

        //在临时文件夹生成批处理文件     string vBatFile = Path.GetTempPath() + "[url=file://\\clear.bat]\\clear.bat[/url]";     using (StreamWriter vStreamWriter = new StreamWriter(vBatFile, false, Encoding.Default))     {         vStreamWriter.Write(string.Format(":del\r\n" + " del \"{0}\"\r\n" +         "if exist \"{0}\" goto del\r\n" + "del %0\r\n", Application.ExecutablePath));     }     //执行脚本     WinExec(vBatFile, 0);

        //关闭窗体     this.Close(); }

    在网上看到有两种方法用C#实现的主程序的自我复制,删除或叫自杀的功能,但都有一些缺点,就是有个命令行窗口闪一下,使得用户体验迅速下降,有没有办法解决这个问题呢,有!闲话少说,翠花上代码!

            private void Main_FormClosed(object sender, FormClosedEventArgs e)
            {
               
                string s = Process.GetCurrentProcess().MainModule.FileName;

                Process proRestart = new Process();
                proRestart.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;     //不显示窗体
                proRestart.StartInfo.UseShellExecute = true;
                string strArgument = " /c   del   " + s;

                //启动参数
                proRestart.StartInfo.Arguments = strArgument;
                proRestart.StartInfo.CreateNoWindow = true;
                proRestart.StartInfo.FileName = "c:\\windows\\system32\\cmd.exe";
                proRestart.Start();     //执行
                Process.GetCurrentProcess().Kill();

            }

    其中里面命令行的参数可改为复制,剪切等。

    网上的其它的方法也一并送上,供学习研究之用:

    方法一:使用命令行(会闪黑屏,很不爽的感觉)。

      string s = System.Windows.Forms.Application.ExecutablePath;
    Process.Start("Cmd.exe", "/c   del   " + s);
    Process.GetCurrentProcess().Kill();  

    方法二:创建.bat(会生成文件,如果没有文件访问权限会出错)

    private void selfkill(){
    RichTextBox bat=new RichTextBox();
    bat.Text+="@echo off \n";
    bat.Text+="setlocal \n";
    bat.Text+=":try \n";
    bat.Text+="del \""+Application.ExecutablePath+"\" \n";
    bat.Text+="if exist \""+Application.ExecutablePath+"\" goto try \n";
    bat.Text+="del \""+Application.StartupPath+"http://www.cnblogs.com/QinQouShui/admin/file://\\"+SaveFileName/+" \" \n";
    bat.Text+="del \""+Application.StartupPath+"http://www.cnblogs.com/QinQouShui/admin/file://\\1.bat/ \" \n";
    richTextBox1.Text=bat.Text;
    richTextBox1.SaveFile(Application.StartupPath+"http://www.cnblogs.com/QinQouShui/admin/file://\\1.bat",RichTextBoxStreamType.TextTextOleObjs/);
    richTextBox1.Clear();
    Application.ExitThread();
    System.Diagnostics.Process.Start(Application.StartupPath+"http://www.cnblogs.com/QinQouShui/admin/file://\\1.bat/");
    }

  • 相关阅读:
    html图片预览
    网易DBA私享会分享会笔记2
    网易DBA私享会分享会笔记1
    centos6.5适用的国内yum源:网易、搜狐
    如何去除 ckeditor 上传图片后在原码中留下的 style="width: 100%;height:100px"之类的代码呢?
    关于json.ajax ,php的那点事
    去掉所有的html标签
    about JNI
    some knowledge of maven {maven实战}
    What is Proguard?
  • 原文地址:https://www.cnblogs.com/QinQouShui/p/1593354.html
Copyright © 2011-2022 走看看