代碼
private void CreateNewFile(string cppName)
{
this.textBox2.Clear();
////////////////////
//讀模板檔案
string strFileName = this.txtTempFile.Text.Trim();
if (!File.Exists(strFileName)) //如果不檔案存在,則拋出異常
{
MessageBox.Show("{0} does not exist!", strFileName);
}
Stream s = new FileStream(strFileName, FileMode.Open);
StreamReader sr = new StreamReader(s, Encoding.Default);
textBox1.Text = sr.ReadToEnd().ToString();
sr.Close();
s.Close();
////////////////////
//組合檔案內容
string strOld = textBox1.Text.Trim();
string strCPPName = strOld.Replace("@_funName", cppName);//"fmmm_05_del"); //更換名稱
string strTableName = strCPPName.Replace("@_tableName",txtTableName.Text); //"TFMMM05");//更改表名稱
string strFunDesc = strTableName.Replace("@_funDescript",txtFunDescript.Text); //"單位組管理");//功能描述
string strCreater = strFunDesc.Replace("@_creater", txtCreater.Text);//"***");//建立者
string strCreateTime = strCreater.Replace("@_createTime", txtCreateTime.Value.ToString("yyyy-MM-dd"));//DateTime.Now.ToString("yyyy-MM-dd"));//建立時間
string strSQL = string.Empty;
if (cppName.Contains("ins"))
{
strSQL = "insert into dbo.TFMMM07(*) values(*)";
}
else if (cppName.Contains("upd"))
{
strSQL = "update dbo.TFMMM07 set * where rec_id=@rec_id";
}
else if (cppName.Contains("del"))
{
strSQL = "delete from dbo.@_tableName where rec_id=@rec_id";
}
else
{
strSQL = "select * from dbo.@_tableName ";
}
string strNewSQL = strSQL.Replace("@_tableName", txtTableName.Text);
string strResult = strCreateTime.Replace("@_sqlString", strNewSQL);
this.textBox2.Text = strResult;
////////////////////
//寫檔案
//執行個體化一個檔案流--->與寫入檔案相關聯
FileStream fs = new FileStream(this.txtDestFilePath.Text+cppName+".cpp", FileMode.Create);
StreamWriter sw = new StreamWriter(fs);
sw.Write(this.textBox2.Text);
sw.Flush();
sw.Close();
fs.Close();
}
1。不停的賦值粘貼,修改對應的部分,直接來個檔案讀寫,把需要修改的部分用關鍵字替換掉
2。讀Stream s = new FileStream(strFileName, FileMode.Open);
StreamReader sr = new StreamReader(s, Encoding.Default);
textBox1.Text = sr.ReadToEnd().ToString();
3。替換,replace
4.寫: FileStream fs = new FileStream(this.txtDestFilePath.Text+cppName+".cpp", FileMode.Create);
StreamWriter sw = new StreamWriter(fs);
sw.Write(this.textB ox2.Text);
sw.Flush();
sw.Close();
fs.Close();