批量C#与VB.NET代码互转

本工具通过在线服务实现VB.NET与C#代码的相互转换,支持批量处理文件夹内的所有文件,自动保存转换后的代码到指定文件夹。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 /* create by     : wgscd
  * date          : 2009/2/12
  * discription   : can bat convert vb codes to c#,and c# to vb.net.
  * Blog          : http://blog.csdn.net/wgsnet
 */

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections;

namespace VB_Cshap
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        public enum ConvertToType
        {
            VB = 1,
            Csharp = 2
        }
        ConvertToType toType = ConvertToType.VB;

        string fliters = "*.cs";
        string sourceExtention = ".cs";
        string desExtention = ".vb";
        bool canSubmit = false;
        string f_sourceFolder = "";  //source folder
        string f_desFolder = ""; //the folder to store the Converted files

        string PostUrl = "http://www.developerfusion.com/tools/convert/csharp-to-vb/";


        //"http://www.developerfusion.com/tools/convert/vb-to-csharp/";


        private void BtnBrow_Click(object sender, EventArgs e)
        {


            FolderBrowserDialog fd = new FolderBrowserDialog();
            if (fd.ShowDialog() == DialogResult.OK)
            {


                f_sourceFolder = fd.SelectedPath;
                textBox1.Text = f_sourceFolder;


            }


        }

 

        private void BtnStart_Click(object sender, EventArgs e)
        {

            f_sourceFolder = textBox1.Text.Trim();
            f_desFolder = textBox2.Text.Trim();
            if (f_sourceFolder == "")
            {


                MessageBox.Show("please choose source path!");
                textBox1.Focus();
                return;

            }
            if (f_desFolder == "")
            {


                MessageBox.Show("please choose save path!");
                textBox2.Focus();
                return;


            }

            if (toType == ConvertToType.VB)
            {

                PostUrl = "http://www.developerfusion.com/tools/convert/csharp-to-vb/";
                fliters = "*.cs";
                sourceExtention = ".cs";
                desExtention = ".vb";
            }

            if (toType == ConvertToType.Csharp)
            {

                PostUrl = "http://www.developerfusion.com/tools/convert/vb-to-csharp/";

                fliters = "*.vb";
                sourceExtention = ".vb";
                desExtention = ".cs";
            }

            webBrowser1.Navigate(PostUrl);
            canSubmit = false;

            listBox1.Items.Clear();
            progressBar1.Value = 0;
            string[] Files = getAllDesFolderFile();
            int FileCount = Files.Length;
            progressBar1.Maximum = FileCount;
            int i = 0;

            if (Files == null)
            {
                MessageBox.Show("No file found in that select folder!");

                return;
            }


            foreach (string file in Files)
            {

                listBox1.SelectedIndex = i;
          
                i++;
                progressBar1.Value = i;
                // if the file is the needed be convert file
                if (file.ToLower().EndsWith(sourceExtention))
                {


                    while (webBrowser1.IsBusy || canSubmit == false)
                    {

                        Application.DoEvents();

                    }
                    this.Text = "Convert file : " + file;
                    PostCode(file);  //post to convert need convert files
                }

                else   //donot need convert files
                {

                    this.Text = "Copy file : " + file;
                    copyToDesFolder(file);  //just copy to des folder

                }

                if (i == FileCount)
                {
                    progressBar1.Value = 0;
                    this.Text = "Convert completed!";
                    MessageBox.Show("Convert completed!");

                }


            }

 

 


        }

 


        string[] getSourceFiles(string _rootFolderPath, string _strFliter)
        {


            return Directory.GetFiles(_rootFolderPath, _strFliter);


        }

 


        ArrayList FileList = new ArrayList();
        void GetAllFile(string strFolder)
        {

            string[] Folders = Directory.GetDirectories(strFolder);
            string[] files = Directory.GetFiles(strFolder, "*.*");  //all files

            foreach (string strFile in files)
            {


                FileList.Add(strFile);
                listBox1.Items.Add(strFile);

            }


            foreach (string str in Folders)
            {
                if (Folders.Length > 0)
                {
                    GetAllFile(str);
                }
            }


        }

 

        string[] getAllDesFolderFile()
        {
            string[] strFiles = null;
            FileList.Clear();
            GetAllFile(f_sourceFolder);

            if (FileList.Count <= 0) { return null; }

            strFiles = new string[FileList.Count];
            for (int i = 0; i < FileList.Count; i++)
            {
                strFiles.SetValue(FileList[i], i);
            }

            return strFiles;

        }

 

 

 


        void PostCode(string _SourFile)
        {


            string sourceCodes = readFile(_SourFile);
         
            HtmlDocument doc = webBrowser1.Document;
            // HtmlElement el = new HtmlElement();

            doc.GetElementById("Code").InnerText = sourceCodes;

            HtmlElement SubmitButton = null;


            foreach (HtmlElement bt in doc.GetElementsByTagName("input"))
            {
                //find the submit button
                if (bt.GetAttribute("value").ToString() == "Convert to C#" || bt.GetAttribute("value").ToString() == "Convert to VB.NET")
                {

                    SubmitButton = bt;

                    break;

                }


            }

 

            SubmitButton.InvokeMember("click"); //do click
            canSubmit = false;
            //wait for post
            while (webBrowser1.IsBusy || canSubmit == false)
            {
                Application.DoEvents();

            }
            //get return document (if converted success the the document should include the converted codes)
            doc = webBrowser1.Document;

            foreach (HtmlElement lk in doc.Links)
            {
                //find the link copy to clipboard in the document
                if (lk.InnerText == "copy to clipboard")
                {

                    //MessageBox.Show("Found link copy to cliboard");
                    //clear the clipboard
                    Clipboard.Clear();
                    //copy to clipboard
                    lk.InvokeMember("click");
                    //pause 2 second
                    pauseSeconds(2000);

                    if (Clipboard.ContainsText()) //if clipboard is nut null
                    {
                        // MessageBox.Show(Clipboard.GetText());//get the converted string on clipboard.
                        string ConvertCodes = Clipboard.GetText();
                        WriteToFile(_SourFile, ConvertCodes); //save to des file

 


                    }

 


                }
            }

 

        }


        private void copyToDesFolder(string _SourFile)
        {


            try
            {
                string desPath = f_desFolder + _SourFile.Substring(f_sourceFolder.Length, _SourFile.Length - f_sourceFolder.Length);
                //MessageBox.Show(desPath);
                createFolder(Path.GetDirectoryName(desPath));
                File.Copy(_SourFile, desPath, true);
            }
            catch (Exception ex)
            {


                MessageBox.Show(ex.Message);


            }

        }

 

        private void WriteToFile(string _SourFile, string Codes)
        {


            try
            {

 


                string desPath = f_desFolder + _SourFile.Substring(f_sourceFolder.Length, _SourFile.Length - f_sourceFolder.Length);
                desPath = desPath.Substring(0, desPath.Length - 3) + desExtention;
                //MessageBox.Show(desPath);
                createFolder(Path.GetDirectoryName(desPath));
                Stream oStream = File.Open(desPath, FileMode.OpenOrCreate);
                StreamWriter oWriter = new StreamWriter(oStream, Encoding.Default);
                oWriter.Write(Codes);
                oWriter.Close();
                oStream.Close();

            }
            catch (Exception ex)
            {


                MessageBox.Show(ex.Message);

            }

        }

        void createFolder(string strFolderName)
        {
            if (Directory.Exists(strFolderName) == false)
            {
                Directory.CreateDirectory(strFolderName);
                // MessageBox ("create Folder: " + strFolderName);
            }
        }

        string readFile(string _SourFile)
        {

            try
            {
                Stream oStream = File.Open(_SourFile, FileMode.OpenOrCreate);
                StreamReader oReader = new StreamReader(oStream, Encoding.Default);
                string _StrReturn = oReader.ReadToEnd();
                oReader.Close();
                oStream.Close();
                return _StrReturn;
            }


            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);

                return "";

            }

        }


        void pauseSeconds(int Minsec)
        {

            DateTime t1 = DateTime.Now.AddMilliseconds(Minsec);
            while (true)
            {

                Application.DoEvents();

                if (DateTime.Now >= t1)
                {
                    break;

                }


            }


        }

        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {

            canSubmit = true;

 


        }

        private void Form1_Load(object sender, EventArgs e)
        {

            f_sourceFolder = @"C:/temp/APP_Convert_VB";
            f_desFolder = @"C:/convert21";
            textBox1.Text = f_sourceFolder;
            textBox2.Text = f_desFolder;

            comboBoxCType.SelectedIndex = 0;
            webBrowser1.Navigate(PostUrl);
            canSubmit = false;


        }

        private void BtnDesFolder_Click(object sender, EventArgs e)
        {

            FolderBrowserDialog fd = new FolderBrowserDialog();
            if (fd.ShowDialog() == DialogResult.OK)
            {


                f_desFolder = fd.SelectedPath;
                textBox2.Text = f_desFolder;


            }

 


        }

        private void comboBoxCType_SelectedIndexChanged(object sender, EventArgs e)
        {


            toType = comboBoxCType.SelectedIndex == 0 ? ConvertToType.VB : ConvertToType.Csharp;

 

        }

 

    }
}

 

 

 下载地址:

 http://download.csdn.net/source/1007749

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值