📄 form1.cs
字号:
private void button1_Click(object sender, System.EventArgs e) //#region
{
try
{
strOut = "";
FindFilesFromDir(txtSourPath.Text,"*",txtExt.Text);
WriteExcelFile(txtOutPath.Text,strOut);
MessageBox.Show("成功");
}
catch (Exception ee)
{
}
} //#endregion
private void FindFilesFromDir(string path,string dirStr,string fileStr) //#region
{
try
{
DirectoryInfo di = new DirectoryInfo(path);
FileSystemInfo[] dirs = di.GetDirectories(dirStr);
string [] arr = fileStr.Split(new Char [] {'|'});
for (int i = 0;i< arr.Length;i++)
{
FileInfo[] fi = di.GetFiles(arr[i]);
foreach (FileInfo fiTemp in fi)
{
strOut = strOut + LoadFile(path + fiTemp.Name);// + "\r\n";
//是否空行
}
}
foreach (DirectoryInfo diNext in dirs)
{
FindFilesFromDir(diNext.FullName + "\\",dirStr,fileStr);
}
}
catch (Exception ee)
{
string str =ee.Message;
}
} //#endregion
private ArrayList FindChineseStr(string source) //#region
{
ArrayList myAll = new ArrayList();
int iCode = 0;
string [] arr = source.Split(new Char [] {'"'});
try
{
for (int i = 0;i< arr.Length;i++)
{
if (i % 2 == 0)
{
ArrayList mySingle = FindChineseStrPure(arr[i]);
for (int m = 0; m< mySingle.Count; m++)
{
myAll.Add(mySingle[m]);
}
}
else
{
for (int j = 0;j< arr[i].Length;j++)
{
iCode = (arr[i][j]);
if (iCode > 255)
{
myAll.Add(arr[i]);
break;
}
}
}
}
}
catch (Exception ee)
{
string str =ee.Message;
}
return myAll;
} //#endregion
private ArrayList FindChineseStrPure(string source) //#region
{
ArrayList myAL = new ArrayList();
int iCode = 0;
int ibegin = -1;
try
{
for(int i = 0; i<source.Length; i++)
{
iCode = (source[i]);
if (iCode == 12288) //0x3000
continue;
if ((iCode <= 255)&&(ibegin != -1))
{
myAL.Add(source.Substring(ibegin,i-ibegin));
ibegin = -1;
}
if ((iCode > 255)&&(ibegin == -1))
{
ibegin = i;
}
if ((i == source.Length - 1)&&(ibegin != -1))
{
myAL.Add(source.Substring(ibegin,i - ibegin + 1));
}
}
}
catch (Exception ee)
{
string str =ee.Message;
}
return myAL;
} //#endregion
private string LoadFile(string path) //#region
{
string resultStr = "";
try
{
using (StringReader sr = new StringReader(ReadFile(path)))
{
String line;
int lineNum = 0;
bool bDisable = false;
string lineStr = "";
while ((line = sr.ReadLine()) != null)
{
lineNum ++;
lineStr = line;
if (lineStr.IndexOf("//") != -1)
{
lineStr = line.Substring(0,line.IndexOf("//"));
}
if (bDisable)
{
if (lineStr.IndexOf("*/") != -1)
{
lineStr = lineStr.Substring(lineStr.IndexOf("*/") + 2);
bDisable = false;
}
else
{
lineStr = "";
}
}
else
{
if (lineStr.IndexOf("/*") != -1)
{
lineStr = lineStr.Substring(0,lineStr.IndexOf("/*"));
bDisable = true;
}
}
if (lineStr.IndexOf("--") != -1)
{
lineStr = line.Substring(0,line.IndexOf("--"));
}
if (lineStr.IndexOf("#region") != -1)
{
lineStr = line.Substring(0,line.IndexOf("#region"));
}
if (lineStr.IndexOf("#endregion") != -1)
{
lineStr = line.Substring(0,line.IndexOf("#endregion"));
}
if (lineStr != "")
{
ArrayList myAL = FindChineseStr(lineStr);
if (myAL.Count > 0)
{
for (int i = 0; i< myAL.Count; i++)
{
if ( !myAL[i].Equals("宋体"))
{
resultStr = resultStr + myAL[i] + "\t" + path + "\t" + lineNum.ToString() + "\t" + path.Substring(path.IndexOf(".")) + "\t" + "\r\n";
}
}
//resultStr = resultStr + "\r\n";
}
}
}
}
}
catch (Exception ee)
{
string str =ee.Message;
}
return resultStr;
} //#endregion
//读文件
public string ReadFile(string filename) //#region
{
string returnStr = "";
System.IO.FileStream fs = new FileStream(filename,FileMode.Open,FileAccess.Read,FileShare.ReadWrite);
try
{
//FileInfo fi=new FileInfo(filename);
//FileStream fs=fi.OpenRead();
int nBytes=(int)fs.Length;
byte[] ByteArray=new byte[nBytes];
int nBytesRead=fs.Read(ByteArray, 0, nBytes);
if (filename.Substring(filename.IndexOf(".")).ToUpper() == ".RESX")
{
returnStr=Encoding.GetEncoding("utf-8").GetString(ByteArray);
}
else
{
returnStr=Encoding.GetEncoding(0).GetString(ByteArray);
}
}
catch{}
finally
{
fs.Close();
}
return returnStr;
} //#endregion
//读文件
public string ReadFile(string filename,string senc) //#region
{
string returnStr = "";
System.IO.FileStream fs = new FileStream(filename,FileMode.Open,FileAccess.Read,FileShare.ReadWrite);
try
{
//FileInfo fi=new FileInfo(filename);
//FileStream fs=fi.OpenRead();
int nBytes=(int)fs.Length;
byte[] ByteArray=new byte[nBytes];
int nBytesRead=fs.Read(ByteArray, 0, nBytes);
returnStr=Encoding.GetEncoding(senc).GetString(ByteArray);
}
catch{}
finally
{
fs.Close();
}
return returnStr;
} //#endregion
//写Excel文件
public void WriteExcelFile(string path,string source) //#region
{
string resultStr = "中文名" + "\t" + "文件名" + "\t" + "行号" + "\t" + "扩展名" + "\t" + "英文名"+"\t"+ "\r\n";
resultStr = resultStr + source;
string senc = "";
try
{
if (checkBox1.Checked==true) //#region
{
senc ="gb2312";
}
else
{
senc ="utf-8";
} //#endregion
byte[] output = Encoding.GetEncoding(senc).GetBytes(resultStr);
System.IO.FileStream fs = new FileStream(path + "output.xls" ,FileMode.Create,FileAccess.Write,FileShare.ReadWrite);
fs.Write(output,0,output.Length);
fs.Close();
}
catch{}
} //#endregion
//写文件
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -