Just Code‎ > ‎

Upgrade from vb6 to vb.net wizzard, to many damned Upgrade warnings? No problem

posted Oct 23, 2009, 5:20 AM by Peter Henell   [ updated Oct 27, 2009, 3:14 AM ]
class Program
    {
        static void Main(string[] args)
        {
            Encoding enc = Encoding.UTF8;
            string outputFolder = "c:\\temp\\bort\\";
            FileInfo[] files = new DirectoryInfo(@"C:\Source\Project1.NET").GetFiles();
            foreach (var item in files)
            {
                if (item.Extension != ".vbproj" && item.Extension != ".sln" && item.Extension != ".suo")
                {
                    StreamWriter writer = new StreamWriter(outputFolder + item.Name, false, Encoding.UTF8);
                    foreach (var line in File.ReadAllLines(item.FullName, Encoding.GetEncoding("iso-8859-1")))
                    {
                        if (!line.Contains("'UPGRADE"))
                        {
                            if (line.ToLower().Contains("gosub"))
                                writer.WriteLine("'" + line);
                            else 
                                writer.WriteLine(ProcessLine(line));
                        }

                        writer.Flush();
                    }
                    writer.Close();
                }
                Console.Write(".");
            }
        }

        private static string ProcessLine(string line)
        {
            // remove VB6 quick field accessors like: RS1!a71 
            // replace with RS1("a71").Value

            Regex r = new Regex(@"(?<rs>RS\d)!(?<id>[a-öA-Ö0-9_]*)", RegexOptions.Compiled);

            if (r.IsMatch(line))
            {
                Match m = r.Match(line);
                string fieldName = m.Result("${id}");
                string resultsetName = m.Result("${rs}");
                
                string res = r.Replace(line, string.Format("{0}(\"{1}\").Value", resultsetName, fieldName));
                return res;
            }

            return line;
        }
    }
Comments