📄 createhtml.cs
字号:
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;
namespace CreateHtml
{
public partial class CreateHtml : Form
{
string[] strExtName = null;
private StringBuilder sb = new StringBuilder();
private string newStrFolder = string.Empty;
public CreateHtml()
{
InitializeComponent();
}
private void CreateHtml_Load(object sender, EventArgs e)
{
}
private void btnFolder_Click(object sender, EventArgs e)
{
FolderDialog.ShowDialog();
txbFolder.Text = FolderDialog.SelectedPath;
}
private void btnClear_Click(object sender, EventArgs e)
{
string strType = txbType.Text.Trim();
string strFolder = txbFolder.Text.Trim();
if (strType.Length == 0)
{
MessageBox.Show("请输入文件类型");
return;
}
else if (strFolder.Length == 0)
{
MessageBox.Show("请选择文件夹");
return;
}
else if (!Directory.Exists(strFolder))
{
MessageBox.Show("文件夹不存在");
return;
}
else if (string.IsNullOrEmpty(Path.GetDirectoryName(strFolder)))
{
MessageBox.Show("不能选择根目录");
return;
}
else
{
strExtName = strType.Split(',');
this.txbMsg.Text = string.Empty;
this.txbMsg.Text += "\n生成HTML";
this.txbMsg.Text += "\n开始处理......";
try
{
getClearNullAllFile(strFolder);
}
catch (Exception ex)
{
MessageBox.Show(this, "Error Message:" + ex.Message);
}
this.txbMsg.Text += "\n处理完成";
}
}
protected void getAllFile(string txtDirPath)
{
DirectoryInfo info3 = new DirectoryInfo(txtDirPath);
foreach (FileSystemInfo info4 in info3.GetFileSystemInfos())
{
if (info4 is FileInfo)
{
FileInfo info = (FileInfo)info4;
string str = info.DirectoryName + @"\" + info.Name;
string extension = info.Extension;
for (int i = 0; i < this.strExtName.Length; i++)
{
if (this.strExtName[i] == extension.ToLower())
{
this.modifyFile(info.DirectoryName + @"\", info.Name, extension);
}
}
}
else
{
DirectoryInfo info2 = (DirectoryInfo)info4;
this.getAllFile(info2.FullName + @"\");
}
}
}
protected void modifyFile(string txtFileDir, string txtFileName, string txtFileType)
{
if (((txtFileName == string.Empty) || (txtFileType == "")) || (txtFileDir == ""))
{
this.txbMsg.Text = "参数错误";
}
else
{
string path = txtFileDir + txtFileName;
txtFileName = txtFileName.Substring(0, txtFileName.Length - txtFileType.Length);
txtFileName = txtFileName.Replace(".", "\x00b7") + "(" + txtFileType.ToLower().Substring(1, txtFileType.Length - 1) + ")";
string str3 = ".htm";
string str4 = txtFileDir + txtFileName + str3;
// this.txbMsg.Text += "\n" + path + "->>" + str4;
this.sb.Append(str4);
StreamReader reader = new StreamReader(path, Encoding.GetEncoding("gb2312"));
StringBuilder builder = new StringBuilder();
while (!reader.EndOfStream)
{
string str5 = reader.ReadLine();
if (str5.Trim().Length > 0)
{
builder.Append("$br");
builder.AppendLine(str5);
}
}
reader.Close();
StreamWriter writer = new StreamWriter(str4, false, Encoding.GetEncoding("gb2312"));
writer.WriteLine("<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01//EN' 'http://www.w3.org/TR/html4/strict.dtd'>");
writer.WriteLine("<html>");
writer.WriteLine("<head>");
writer.WriteLine("<meta http-equiv='Content-Type' content='text/html; charset=gb2312'>");
writer.WriteLine("<title>" + txtFileName + "</title>");
writer.WriteLine("</head>");
writer.WriteLine("<style>");
writer.WriteLine("body{ white-space: pre; }");
writer.WriteLine("</style>");
writer.WriteLine("<body>");
writer.Write(builder.ToString().Replace("<", "<").Replace(">", ">").Replace("$br", "<br />"));
writer.WriteLine(" ");
writer.WriteLine("</body>");
writer.WriteLine("</html>");
writer.Close();
}
}
private void btnHtml_Click(object sender, EventArgs e)
{
string strType = txbType.Text.Trim();
string strFolder = txbFolder.Text.Trim();
if (strType.Length == 0)
{
MessageBox.Show("请输入文件类型");
return;
}
else if (strFolder.Length == 0)
{
MessageBox.Show("请选择文件夹");
return;
}
else if (!Directory.Exists(strFolder))
{
MessageBox.Show("文件夹不存在");
return;
}
else if (string.IsNullOrEmpty(Path.GetDirectoryName(strFolder)))
{
MessageBox.Show("不能选择根目录");
return;
}
else
{
strExtName = strType.Split(',');
this.txbMsg.Text = string.Empty;
this.txbMsg.Text += "\n生成HTML";
this.txbMsg.Text += "\n开始处理......";
try
{
getAllFile(strFolder);
}
catch (Exception ex)
{
MessageBox.Show(this, "Error Message:" + ex.Message);
}
this.txbMsg.Text += "\n处理完成";
}
}
protected void getClearNullAllFile(string txtDirPath)
{
DirectoryInfo info3 = new DirectoryInfo(txtDirPath);
foreach (FileSystemInfo info4 in info3.GetFileSystemInfos())
{
if (info4 is FileInfo)
{
FileInfo info = (FileInfo)info4;
string str = info.DirectoryName + @"\" + info.Name;
string extension = info.Extension;
for (int i = 0; i < this.strExtName.Length; i++)
{
if (this.strExtName[i] == extension.ToLower())
{
this.ClearNull(info.DirectoryName + @"\", info.Name, extension);
}
}
}
else
{
DirectoryInfo info2 = (DirectoryInfo)info4;
this.getClearNullAllFile(info2.FullName + @"\");
}
}
}
private void ClearNull(string txtFileDir, string txtFileName, string txtFileType)
{
StringBuilder htmltext = new StringBuilder();
string strFile = txtFileDir + txtFileName;
using (StreamReader reader = new StreamReader(strFile, System.Text.Encoding.GetEncoding("gb2312")))
{
while (!reader.EndOfStream)
{
string str5 = reader.ReadLine();
if (str5.Trim().Length > 0)
{
htmltext.AppendLine(str5);
}
}
reader.Close();
}
this.txbMsg.Text += "\n" + strFile;
using (StreamWriter sw = new StreamWriter(strFile, false, System.Text.Encoding.GetEncoding("gb2312")))
{
sw.WriteLine(htmltext);
sw.Flush();
sw.Close();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -