代码
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();
}
{
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();