namespace SysUpdate
{
public
partial
class frmSysUpdateUp : Form
{
private
string updateUrl =
string.Empty;
private
string tempUpdatePath =
string.Empty;
WinBase.Common W1 =
new WinBase.Common();
public frmSysUpdateUp()
{
InitializeComponent();
}
private
void frmSysUpdateUp_Load(
object sender, EventArgs e)
{
//
数据库SysUpdate表 显示需要更新内容
dgvList.ColumnCount =
9;
dgvList.AutoGenerateColumns =
false;
DataGridViewCheckBoxColumn newColumn =
new DataGridViewCheckBoxColumn();
DataGridViewCheckBoxColumn newColumn1 =
new DataGridViewCheckBoxColumn();
DataGridViewCheckBoxColumn newColumn2 =
new DataGridViewCheckBoxColumn();
dgvList.Columns.Insert(
0, newColumn);
dgvList.Columns.Insert(
10, newColumn1);
dgvList.Columns.Insert(
11, newColumn2);
this.SetdgvListHeadText();
string strSQL41 =
"
SELECT * FROM SysUpdate order by ID;
";
DataSet ds = W1.DS(strSQL41,
"
Sys
");
dgvList.DataSource = ds.Tables[
0].DefaultView;
this.DataBinding();
dgvList.Columns[
0].Frozen =
true;
dgvList.Columns[
1].Frozen =
true;
dgvList.Columns[
2].Frozen =
true;
dgvList.Columns[
3].Frozen =
true;
//
dgvList.Columns[4].Frozen = true;
}
private
void SetdgvListHeadText()
{
dgvList.Columns[
0].HeaderText =
"
选择
";
dgvList.Columns[
0].Width =
50;
dgvList.Columns[
1].HeaderText =
"
ID
";
dgvList.Columns[
1].Width =
20;
dgvList.Columns[
1].ReadOnly =
true;
dgvList.Columns[
2].HeaderText =
"
组件名
";
dgvList.Columns[
2].Width =
120;
dgvList.Columns[
2].ReadOnly =
true;
dgvList.Columns[
3].HeaderText =
"
版本号
";
dgvList.Columns[
3].Width =
80;
dgvList.Columns[
3].ReadOnly =
true;
dgvList.Columns[
4].HeaderText =
"
大小
";
dgvList.Columns[
4].Width =
100;
dgvList.Columns[
4].ReadOnly =
true;
dgvList.Columns[
5].HeaderText =
"
创建日期
";
dgvList.Columns[
5].Width =
150;
dgvList.Columns[
5].ReadOnly =
true;
dgvList.Columns[
6].HeaderText =
"
哈希值
";
dgvList.Columns[
6].Width =
250;
dgvList.Columns[
6].ReadOnly =
true;
dgvList.Columns[
7].HeaderText =
"
MD5值
";
dgvList.Columns[
7].Width =
250;
dgvList.Columns[
7].ReadOnly =
true;
dgvList.Columns[
8].HeaderText =
"
目录
";
dgvList.Columns[
8].Width =
80;
dgvList.Columns[
8].ReadOnly =
true;
dgvList.Columns[
9].HeaderText =
"
文件类型
";
dgvList.Columns[
9].Width =
80;
dgvList.Columns[
9].ReadOnly =
true;
dgvList.Columns[
10].HeaderText =
"
是否更新
";
dgvList.Columns[
10].Width =
80;
//
dgvList.Columns[10].ReadOnly = true;
dgvList.Columns[
11].HeaderText =
"
是否需要注册
";
dgvList.Columns[
11].Width =
110;
//
dgvList.Columns[11].ReadOnly = true;
}
private
string Byte2String(
byte[] arrByte)
{
StringBuilder sb =
new StringBuilder();
foreach (
byte b
in arrByte)
{
sb.Append(b >
15 ? Convert.ToString(b,
16) :
'
0
' + Convert.ToString(b,
16));
}
return sb.ToString();
}
private
void DataBinding()
{
//
ID, FileName, FileVersion, FilePath, FileData, FileSize, FileDate, FileType, FilesHash, FilesMD5, UpdateBit, RegBit
dgvList.Columns[
0].DataPropertyName =
"
Check1
";
dgvList.Columns[
1].DataPropertyName =
"
ID
";
dgvList.Columns[
2].DataPropertyName =
"
FileName
";
dgvList.Columns[
3].DataPropertyName =
"
FileVersion
";
dgvList.Columns[
4].DataPropertyName =
"
FileSize
";
dgvList.Columns[
5].DataPropertyName =
"
FileDate
";
dgvList.Columns[
6].DataPropertyName =
"
FilesHash
";
dgvList.Columns[
7].DataPropertyName =
"
FilesMD5
";
dgvList.Columns[
8].DataPropertyName =
"
FilePath
";
dgvList.Columns[
9].DataPropertyName =
"
FileType
";
dgvList.Columns[
10].DataPropertyName =
"
UpdateBit
";
dgvList.Columns[
11].DataPropertyName =
"
RegBit
";
}
private
void ListDate()
{
string strSQL41 =
"
SELECT * FROM SysUpdate order by ID ;
";
DataSet ds = W1.DS(strSQL41,
"
Sys
");
dgvList.DataSource = ds.Tables[
0].DefaultView;
this.DataBinding();
}
private
void btnUpload_Click(
object sender, EventArgs e)
{
txtFileText.Text =
"";
openFileDialog1.InitialDirectory =
"
d:\\
";
openFileDialog1.Filter =
"
文件(*.*)|*.*
";
openFileDialog1.FilterIndex =
1;
openFileDialog1.RestoreDirectory =
true;
openFileDialog1.Multiselect =
true;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
txtFileText.Text = openFileDialog1.FileName;
//
文件名称
String S = System.IO.Path.GetFileName(openFileDialog1.FileName);
//
文件格式
//
String FileType = System.IO.Path.GetExtension(openFileDialog1.FileName);
string SHA1 =
"";
string md5 =
"";
SHA1 = FileUtil.SHA1File(txtFileText.Text.ToString());
//
这个消息摘要可以用来验证数据的完整性
md5 = FileUtil.MD5File(txtFileText.Text.ToString());
try
{
if (FileUtil.IsInUse(txtFileText.Text.ToString()))
{
MessageBox.Show(
"
文件已经使用中,无法上传!
",
"
错误提示
", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
if (FileUtil.FileIsReadOnly(txtFileText.Text.ToString()))
{
FileUtil.SetFileReadonly(txtFileText.Text.ToString(),
false);
}
FileStream fs =
new FileStream(txtFileText.Text.ToString(), FileMode.Open);
int streamLength = (
int)fs.Length;
//
获取文件流的长度。
byte[] image =
new
byte[streamLength];
//
声明字节数组,用于保存图片文件
fs.Read(image,
0, streamLength);
//
把图片文件转换成为字节数组保存
fs.Close();
//
文件版本号
FileVersionInfo fileVersion = FileVersionInfo.GetVersionInfo(txtFileText.Text.ToString());
FileInfo file =
new FileInfo(txtFileText.Text.ToString());
//
byte[] args0 = Common.SharpZip.CompressB(image);
byte[] args0 = Common.SharpZip.ICGzipCompress(image);
//
创建文件流,path参数是文件路径
string strSQL2 =
"
exec Insert_UpdateFile '
" + S +
"
','
" + fileVersion.FileVersion +
"
','','
" + file.Length +
"
','
" + file.CreationTime +
"
','
" + file.Extension +
"
','
" + SHA1 +
"
','
" + md5 +
"
',0x
" + Byte2String(args0) +
"";
//
string strSQL2 = "exec Insert_UpdateFile '" + S + "','" + fileVersion.FileVersion + "','','" + file.Length + "','" + file.CreationTime + "','" + file.Extension + "','" + SHA1 + "','" + md5 + "',0x" + Byte2String(image) + "";
int k = W1.DC(strSQL2,
"
Sys
");
if (k>
0)
{
MessageBox.Show(
"
成功!
",
"
成功提示!
", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show(
"
失败!
",
"
错误提示
", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
catch
{
MessageBox.Show(
"
文件插入数据库失败!文件可能正在占用!!
",
"
错误提示
", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
ListDate();
}
}
private
void dgvList_CellClick(
object sender, DataGridViewCellEventArgs e)
{
//
ID, FileName, FileVersion, FilePath, FileData, FileSize, FileDate, FileType, FilesHash, FilesMD5, UpdateBit, RegBit
txtID.Text = dgvList[
1, e.RowIndex].Value.ToString();
txtFilePath.Text = dgvList[
8, e.RowIndex].Value.ToString();
chkUpdateBit.Checked = Convert.ToBoolean(dgvList[
10, e.RowIndex].Value);
chkRegBit.Checked = Convert.ToBoolean(dgvList[
11, e.RowIndex].Value);
}
private
void btnCancel_Click(
object sender, EventArgs e)
{
this.Close();
}
private
void btnSave_Click(
object sender, EventArgs e)
{
string strSQL2 =
"
Update SysUpdate set FilePath='
" + txtFilePath.Text +
"
',UpdateBit='
" + Convert.ToBoolean(chkUpdateBit.Checked) +
"
',RegBit='
" + Convert.ToBoolean(chkRegBit.Checked) +
"
' where ID='
" + txtID.Text +
"
';
";
int k = W1.DC(strSQL2,
"
Sys
");
if (k >
0)
{
MessageBox.Show(
"
更新成功!
",
"
成功提示!
", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show(
"
更新失败!
",
"
错误提示
", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
ListDate();
}
private
void btnDel_Click(
object sender, EventArgs e)
{
string strSQL2 =
"
DELETE SysUpdate Where ID='
" + txtID.Text +
"
';
";
int k = W1.DC(strSQL2,
"
Sys
");
if (k >
0)
{
MessageBox.Show(
"
删除成功!
",
"
成功提示!
", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show(
"
删除失败!
",
"
错误提示
", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
ListDate();
}
private
void dgvList_DataBindingComplete(
object sender, DataGridViewBindingCompleteEventArgs e)
{
for (
int i =
0; i <
this.dgvList.Rows.Count; )
{
this.dgvList.Rows[i].DefaultCellStyle.BackColor = System.Drawing.Color.FromArgb(Convert.ToInt32(W1.LoadXmlFileValue(
"
config.xml
",
"
Color
",
"
IMColor
"))); ;
//
pParentWin.globalcolor2;
i +=
2;
}
}
///
<summary>
///
序列化
///
</summary>
///
<param name="data">
要序列化的对象
</param>
///
<returns>
返回存放序列化后的数据缓冲区
</returns>
public
static
byte[] Serialize(
object data)
{
BinaryFormatter formatter =
new BinaryFormatter();
MemoryStream rems =
new MemoryStream();
formatter.Serialize(rems, data);
return rems.GetBuffer();
}
///
<summary>
///
反序列化
///
</summary>
///
<param name="data">
数据缓冲区
</param>
///
<returns>
对象
</returns>
public
static
object Deserialize(
byte[] data)
{
BinaryFormatter formatter =
new BinaryFormatter();
MemoryStream rems =
new MemoryStream(data);
data =
null;
return formatter.Deserialize(rems);
}
}
}