C# 自杀程序 程序运行完毕后删除自身不显示批处理黑窗口 正常运行

:Repeat
del /f "d:\新建 360压缩 ZIP 文件.zip"
if exist "d:\新建 360压缩 ZIP 文件.zip"  goto Repeat
rmdir "d:\"
del "\DelUS.bat"epeat
rmdir "d:\"
del "\DelUS.bat"

 之前在删除中文文件名的时候,总是不得行,想了很多都没有找到原因,但是同样的内容,新增一个bat,却又能删除,

因为我的是在代码里生成的批处理内容,问题应该是出在这里在,中文,那是不是编码的问题呢?哈哈,还真是这样,完整代码如下:

        private string pathbat
        {
            get { return FileDirecName + @"Aaa.BAT"; }//生成的bat路径
        }
        private string path
        {
            get
            {
                return @"" + this.textBox1.Text.Trim() + "";

            }
        }
        private string FileName
        {
            get
            {
                return Path.GetFileName(path);//取得路径中的文件名

            }
        }
        private string FileDirecName
        {
            get
            {
                return Path.GetDirectoryName(path);//获取路径中的目录,不包含文件名哦

            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            StringBuilder sb = new StringBuilder();
            string text = string.Format("{0}\r\n{1} {2}\r\n{3}\r\n{4}\r\n{5}", ":Repeat", "del /f", "\"" + path + "\"", "if exist \"" + path + "\"  goto Repeat", "rmdir \"" + FileDirecName + "\"", "del \"\\DelUS.bat\"");
            string tex2 = string.Format("{0}\r\n{1} {2}\r\n{3}\r\n{4}\r\n{5}", ":Repeat", "del /f", "\"" + pathbat + "\"", "if exist \"" + pathbat + "\"  goto Repeat", "rmdir \"" + FileDirecName + "\"", "del \"\\DelUS.bat\"");
            sb.Append(text).Append("\r\n").Append(tex2);
            FileStream fs = new FileStream(pathbat, FileMode.Create, FileAccess.Write);
            StreamWriter sw = new StreamWriter(fs, Encoding.GetEncoding("gb2312"));
            sw.Write(sb.ToString());
            sw.Flush();
            sw.Close();
            System.Diagnostics.ProcessStartInfo myStartInfo = new System.Diagnostics.ProcessStartInfo();
            myStartInfo.FileName = pathbat;
            myStartInfo.WorkingDirectory = FileDirecName;
            System.Diagnostics.Process myProcess = new System.Diagnostics.Process();
            myProcess.StartInfo = myStartInfo;
            myProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            myProcess.Start();
            myProcess.WaitForExit(); //等待程序退出
            base.Close();
        }

以上部分代码来源于网络

 

 

posted @ 2013-07-03 11:16  永远的菜鸟@me  阅读(462)  评论(0编辑  收藏  举报