Ok heres some VERY rough VERY crap VERY inefficient code that does a bit of the work. Creates c# compatible namespaces, classname (1 minute of testing says it does anyway).
Also note theres some hard coded paths in for the DEBUG code path. Change em. ALSO THE DEBUG CODE DELETE THE CONTENTS OF THE OUTPUT PATH!! U WERE WARNED.
Post what's missing please. So far I have:
Here it is, hack freely:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Text.RegularExpressions;
namespace ASPXOMatic
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
FolderBrowserDialog selectFolderDialog = new FolderBrowserDialog();
selectFolderDialog.Description = "Select the folder to search for html files.";
string scanPath;
string outputPath;
#if !DEBUG
var result = selectFolderDialog.ShowDialog();
if (result != DialogResult.OK)
return;
scanPath = selectFolderDialog.SelectedPath;
#else
scanPath = "C:\\tmp\\service terminal";
#endif
selectFolderDialog.Description = "Select the folder to output conversions.";
#if !DEBUG
result = selectFolderDialog.ShowDialog();
if (result != DialogResult.OK)
return;
outputPath = selectFolderDialog.SelectedPath;
#else
outputPath = "C:\\tmp\\foo";
new DirectoryInfo(outputPath).Delete(true);
Directory.CreateDirectory(outputPath);
#endif
DirectoryInfo scanFolder = new DirectoryInfo(scanPath);
List workUnit = new List();
workUnit.AddRange(scanFolder.GetFiles("*.html", SearchOption.AllDirectories));
workUnit.AddRange(scanFolder.GetFiles("*.htm", SearchOption.AllDirectories));
string pageDirective = @"<%@ Page Language=""C#"" AutoEventWireup=""true"" CodeBehind=""{0}.cs"" Inherits=""{1}"" Title=""{2}"" %>";
string ns = "MyApp.MyTest";//add some kinda prompter here.
string pageSource =
@"using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
namespace {0}
{{
public partial class {1} : System.Web.UI.Page
{{
}}
}}";
foreach (FileInfo htmlFile in workUnit)
{
string relativePath = htmlFile.DirectoryName.Replace(scanPath + "\\", string.Empty);
string newPath = Path.Combine(outputPath, relativePath);
if (!Directory.Exists(newPath))
{
Directory.CreateDirectory(newPath);
}
string fileNs = ns;
if (!string.IsNullOrEmpty(relativePath))
{
fileNs += "." + relativePath.Replace('\\', '.');
Regex ex = new Regex("\\.(\\d)");
fileNs = ex.Replace(fileNs, "._$1");
ex = new Regex("\\.([a-z])");
fileNs = ex.Replace(fileNs, new MatchEvaluator(p => "." + p.Groups[1].Value.ToUpper()));
}
string name = Path.GetFileNameWithoutExtension(htmlFile.Name);
string className = name;
Regex fex = new Regex("[^A-Za-z\\d]");
className = fex.Replace(className, string.Empty);
fex = new Regex("^(\\d)");
className = fex.Replace(className, "_$1");
string fileSource = htmlFile.OpenText().ReadToEnd();
fex = new Regex("<title>(.+)</title>", RegexOptions.IgnoreCase | RegexOptions.Multiline);
Match m = fex.Match(fileSource);
string title = m.Success ? m.Groups[1].Value : "Untitled Page";
string directive = string.Format(pageDirective, name + ".aspx", fileNs + "." + className, title);
fex = new Regex("<(head)>", RegexOptions.IgnoreCase);
fileSource = fex.Replace(fileSource, "<$1 runat=\"server\">");
StringBuilder newSource = new StringBuilder();
newSource.AppendLine(directive);
newSource.Append(fileSource);
StreamWriter newOut = new StreamWriter(Path.Combine(newPath, name + ".aspx"));
newOut.Write(newSource.ToString());
newOut.Close();
string codeBehind = string.Format(pageSource, fileNs, className);
newOut = new StreamWriter(Path.Combine(newPath, name + ".aspx.cs"));
newOut.Write(codeBehind);
newOut.Close();
}
}
}
}
-- Sam Critchley
"Wise man say 'forgiveness is divine, but never pay full price for late pizza." - TMNT
website designinternet marketing