资源介绍
private void btnCreateEntity_Click(object sender, EventArgs e)
{
HeaderOfEntity HeaderOfEntity = new HeaderOfEntity();
DataTable dt = new DataTable();
#region 实例化一个实体,并为这个实体类赋值
//实体类名不能为空
if (txtClassName.Text == "")
{
MessageBox.Show("类名不能为空!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
else
{
HeaderOfEntity.ClassName = txtClassName.Text.Trim();
}
//实体类的说明
if (txtClassExplain.Text != "")
{
HeaderOfEntity.ClassExplain = txtClassExplain.Text.Trim();
}
else
{
HeaderOfEntity.ClassExplain = "";
}
//实体类的作者
if (txtAuthor.Text != "")
{
HeaderOfEntity.ClassAuthor = txtAuthor.Text.Trim();
}
else
{
HeaderOfEntity.ClassAuthor = "";
}
//类的命名空间
if (txtNamespace.Text != "")
{
HeaderOfEntity.NameSpace = txtNamespace.Text.Trim();
}
else
{
HeaderOfEntity.NameSpace = "";
}
#endregion
#region 将dataGridView中的数据复制出来,复制到DataTable中,做为参数
//添加列
for (int i = 0; i < dgvAttribute.Columns.Count; i++)
{
dt.Columns.Add(dgvAttribute.Columns[i].Name);
}
//添加行
for (int j = 0; j < dgvAttribute.Rows.Count; j++)
{
DataRow dr = dt.NewRow();
for (int k = 0; k < dgvAttribute.Columns.Count; k++)
{
dr[k] = dgvAttribute.Rows[j].Cells[k].Value;
}
dt.Rows.Add(dr);
}
#endregion
string strFileName = "\\" + txtClassName.Text + ".cs";
//实例化一个接口对象
IWriteEntity writeEntity = new WriteEntity();
string strPath = "../../CreateClass";
FileStream fs;
if (!File.Exists(strPath + strFileName))
{
fs = new FileStream(strPath + strFileName, FileMode.Create,FileAccess.Write);
}
else
{
fs = new FileStream(strPath + strFileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
}
if (writeEntity.Write(fs, dt, HeaderOfEntity) == true)
{
MessageBox.Show("写入文件成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("写入文件失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}