好长时间没有学习编程了,但心中一直挂念着程序,怀念过去曾经的时光。
最近工作需要,需要修改一些文件的属性,看到网上有一个,研究一番,觉得还是自己写一个,一来练练手过过瘾,二来也增加一些功能,于是花费了3个晚上写了这个文件属性修改器,(汗,用这么长时间)。
程序中用到了以前不曾使用的三项技术:窗体之间事件捕获、画图和资源管理。
下面是程序代码,资源管理文件无法上传。
//
Form1
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Resources;
using System.Reflection;
namespace FileAttrib
{
/// <summary>
/// 设置文件属性
/// </summary>
public partial class Form1 : Form
{
private Form2 form2;
// button4被单击的次数,用来判断当前应显示哪个文件的属性
int buttonClickCount = 0 ;
private ResourceManager getLocalResource = new ResourceManager( " FileAttrib.Properties.Resources " , Assembly.GetEntryAssembly());
public Form1()
{
// 设置图标
this .Icon = (Icon )getLocalResource.GetObject( " propertiesORoptions " );
InitializeComponent();
}
private void button3_Click( object sender, EventArgs e)
{
Application.Exit();
}
/// <summary>
/// 选择文件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click( object sender, EventArgs e)
{
int fileNameCount = 0 ;
string [] fileNames;
if ( this .openFileDialog1.ShowDialog() == DialogResult.OK)
{
fileNames = this .openFileDialog1.FileNames;
fileNameCount = fileNames.Length;
if (fileNameCount == 1 )
{
this .textBox1.Text = fileNames[ 0 ];
button4.Visible = false ;
// 取文件属性
loadFileAttrib(fileNames[ 0 ]);
}
else
{
// 选择文件多于一个时,打开第二个窗口,以放置文件
if (form2 == null || form2.IsDisposed)
{
form2 = new Form2();
form2.Width = base .Width;
form2.Left = base .Left;
form2.Top = base .Top - form2.Height;
// form2成为form1的附属窗体
this .AddOwnedForm(form2);
form2.Show();
}
// form2移动、修改大小的事件通知,这样两个窗口的相对位置可以保持不动
form2.moveHaveRaise += new Form2.formMoveHandler( this .Form2_Move);
// form2的ListViewItem选择的事件通知
form2.itemSelectChange += new Form2.formMoveHandler( this .itemSelect_Change);
}
// 将选择的文件放到form2中
if (form2 != null )
{
form2.addListViewItems = fileNames;
}
}
}
/// <summary>
/// 修改文件属性
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_Click( object sender, EventArgs e)
{
char [] ss = { ' | ' };
int fileNumber = textBox1.Text.Split(ss).Length;
if (fileNumber == 1 )
{
writeFileAttrib(textBox1.Text);
}
else
{
if (MessageBox.Show( " 你选择了 " + fileNumber.ToString() + " 个文件,这些文件的属性都将被修改,确定吗? " , " 修改确认 " ,M
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Resources;
using System.Reflection;
namespace FileAttrib
{
/// <summary>
/// 设置文件属性
/// </summary>
public partial class Form1 : Form
{
private Form2 form2;
// button4被单击的次数,用来判断当前应显示哪个文件的属性
int buttonClickCount = 0 ;
private ResourceManager getLocalResource = new ResourceManager( " FileAttrib.Properties.Resources " , Assembly.GetEntryAssembly());
public Form1()
{
// 设置图标
this .Icon = (Icon )getLocalResource.GetObject( " propertiesORoptions " );
InitializeComponent();
}
private void button3_Click( object sender, EventArgs e)
{
Application.Exit();
}
/// <summary>
/// 选择文件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click( object sender, EventArgs e)
{
int fileNameCount = 0 ;
string [] fileNames;
if ( this .openFileDialog1.ShowDialog() == DialogResult.OK)
{
fileNames = this .openFileDialog1.FileNames;
fileNameCount = fileNames.Length;
if (fileNameCount == 1 )
{
this .textBox1.Text = fileNames[ 0 ];
button4.Visible = false ;
// 取文件属性
loadFileAttrib(fileNames[ 0 ]);
}
else
{
// 选择文件多于一个时,打开第二个窗口,以放置文件
if (form2 == null || form2.IsDisposed)
{
form2 = new Form2();
form2.Width = base .Width;
form2.Left = base .Left;
form2.Top = base .Top - form2.Height;
// form2成为form1的附属窗体
this .AddOwnedForm(form2);
form2.Show();
}
// form2移动、修改大小的事件通知,这样两个窗口的相对位置可以保持不动
form2.moveHaveRaise += new Form2.formMoveHandler( this .Form2_Move);
// form2的ListViewItem选择的事件通知
form2.itemSelectChange += new Form2.formMoveHandler( this .itemSelect_Change);
}
// 将选择的文件放到form2中
if (form2 != null )
{
form2.addListViewItems = fileNames;
}
}
}
/// <summary>
/// 修改文件属性
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_Click( object sender, EventArgs e)
{
char [] ss = { ' | ' };
int fileNumber = textBox1.Text.Split(ss).Length;
if (fileNumber == 1 )
{
writeFileAttrib(textBox1.Text);
}
else
{
if (MessageBox.Show( " 你选择了 " + fileNumber.ToString() + " 个文件,这些文件的属性都将被修改,确定吗? " , " 修改确认 " ,M