好长时间没有学习编程了,但心中一直挂念着程序,怀念过去曾经的时光。 
         最近工作需要,需要修改一些文件的属性,看到网上有一个,研究一番,觉得还是自己写一个,一来练练手过过瘾,二来也增加一些功能,于是花费了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()+"个文件,这些文件的属性都将被修改,确定吗?","修改确认",MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    foreach (string fileName in textBox1.Text.Split(ss))
                    {
                        writeFileAttrib(fileName);
                    }
                }
            }
        }

/// <summary>
        /// 点击该按钮,顺序显示textBox1中个文件的属性,同时form2.SelectedItems的当前项被用红色的矩形框标示出来
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button4_Click(object sender, EventArgs e)
        {
            char[] ss = { '|' };
            int len, i = 1;
            len = textBox1.Text.Split(ss).Length;
            buttonClickCount++;
            string[] fileNames = textBox1.Text.Split(ss);
            foreach (string fileName in fileNames)
            {
                if (i == buttonClickCount+1)
                {
                    //显示属性
                    loadFileAttrib(fileName);
                    //文件名传递到form2,用来查找相对应的项。
                    form2.flashItem = fileName;
                    if (buttonClickCount+1 == len)
                    {
                        buttonClickCount=-1;
                    }
                    break;
                }
                i++;
            }
        }

/// <summary>
        /// 窗口移动,form2跟着移动
        /// 两个form2.moveHaveRaise事件是为了防止移动事件重复出现导致图像闪烁
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1_Move(object sender, EventArgs e)
        {
            if (form2!= null)
            {
                form2.moveHaveRaise -= new Form2.formMoveHandler(this.Form2_Move);
                form2.Left = base.Left;
                form2.Top = base.Top - form2.Height;
                form2.moveHaveRaise += new Form2.formMoveHandler(this.Form2_Move);
            }
        }

/// <summary>
        /// form2窗口移动,form1跟着移动
        /// 两个this.Move事件是为了防止移动事件重复出现导致图像闪烁
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form2_Move(object sender, EventArgs e)
        {
            if (form2 != null)
            {
                this.Move -= new System.EventHandler(this.Form1_Move);
                this.Left = form2.Left;
                this.Top = form2.Top + form2.Height;
                this.Move += new System.EventHandler(this.Form1_Move);
            }
        }

/// <summary>
        /// form2的ListViewItem选择的事件处理程序
        /// 选择ListViewItem项,相应的将文件名写到textBox1中,文件名之间用"|"分割
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void itemSelect_Change(object sender, EventArgs e)
        {
            textBox1.Text = "";

//SelectedItemArray是SelectedItems的集合数组
            foreach (ListViewItem item in form2.SelectedItemArray)
            {
                textBox1.Text += item.SubItems[1].Text + "|";
            }

char[] ss = {'|'};
            textBox1.Text = textBox1.Text.TrimEnd(ss);//去掉末尾的"|"

if (textBox1.Text.Split(ss).Length == 1)
            {
                loadFileAttrib(textBox1.Text);
                button4.Visible = false;
            }
            else
            {
                button4.Visible = true;
                buttonClickCount = 0;
                loadFileAttrib(textBox1.Text.Split(ss)[0]);
            }
        }

/// <summary>
        /// 取文件属性
        /// </summary>
        /// <param name="fileName"></param>
        private void loadFileAttrib(string fileName)
        {
            this.checkBox1.Checked = false;
            this.checkBox2.Checked = false;
            this.checkBox3.Checked = false;
            this.checkBox4.Checked = false;
            this.checkBox5.Checked = false;
            if (File.Exists(fileName))
            {
                this.dateTimePicker1.Text = File.GetCreationTime(fileName).ToString();
                this.dateTimePicker2.Text = File.GetLastWriteTime(fileName).ToString();
                this.dateTimePicker3.Text = File.GetLastAccessTime(fileName).ToString();
                string text1 = File.GetAttributes(fileName).ToString();
                if (text1.LastIndexOf("ReadOnly") != -1)
                {
                    this.checkBox1.Checked = true;
                }
                if (text1.LastIndexOf("System") != -1)
                {
                    this.checkBox2.Checked = true;
                }
                if (text1.LastIndexOf("Hidden") != -1)
                {
                    this.checkBox3.Checked = true;
                }
                if (text1.LastIndexOf("Archive") != -1)
                {
                    this.checkBox4.Checked = true;
                }
                if (text1.LastIndexOf("Temporary") != -1)
                {
                    this.checkBox5.Checked = true;
                }

FileInfo fi = new FileInfo(fileName);
                label6.Text = fi.Length.ToString("#,#") + "字节";
                fi = null;
            }
        }

/// <summary>
        /// 写文件属性
        /// </summary>
        /// <param name="fileName"></param>
        private void writeFileAttrib(string fileName)
        {
            if (File.Exists(fileName))
            {
                File.SetAttributes(fileName, FileAttributes.Normal);
                FileAttributes attributes1 = File.GetAttributes(fileName);
                if (this.checkBox1.Checked)
                {
                    File.SetAttributes(fileName, FileAttributes.ReadOnly);
                }
                if (this.checkBox2.Checked)
                {
                    File.SetAttributes(fileName, attributes1 | FileAttributes.System);
                }
                if (this.checkBox3.Checked)
                {
                    File.SetAttributes(fileName, attributes1 | FileAttributes.Hidden);
                }
                if (this.checkBox4.Checked)
                {
                    File.SetAttributes(fileName, attributes1 | FileAttributes.Archive);
                }
                if (this.checkBox1.Checked)
                {
                    File.SetAttributes(fileName, attributes1 | FileAttributes.Temporary);
                }
                File.SetCreationTime(fileName, this.dateTimePicker1.Value);
                File.SetLastWriteTime(fileName, this.dateTimePicker2.Value);
                File.SetLastAccessTime(fileName, this.dateTimePicker3.Value);

char[] ss = { '|' };
                int fileNumber = textBox1.Text.Split(ss).Length;
                if (fileNumber == 1 || fileName == textBox1.Text.Split(ss)[fileNumber-1])
                {
                    MessageBox.Show(getLocalResource.GetString("setAttribSuccess"), getLocalResource.GetString("infoTip"), MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                }
            }
            else
            {
                string fileNotExistMessage = getLocalResource.GetString("fileNotExist");
                MessageBox.Show(fileNotExistMessage + ":" + fileName);
            }
        }

private void Form1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            System.Diagnostics.Process.Start("mailto:kfqcharge@126.com");
        }
    }
}

//Form2

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;

namespace FileAttrib
{
    public partial class Form2 : Form
    {
        //定义事件委托
        public delegate void formMoveHandler(object sender, EventArgs e);
        //定义事件,1、窗体移动或大小改变时间;2、ListView项选择事件
        public event formMoveHandler moveHaveRaise,itemSelectChange;

string[] addFileNames;
        int fileNumber = 0;

public Form2()
        {
            InitializeComponent();
        }

/// <summary>
        /// 属性:增加文件,调用addItems
        /// </summary>
        public string[] addListViewItems
        {
            set 
            {
                addFileNames = value;
                addItems(addFileNames);
            }
        }

/// <summary>
        /// 增加文件,调用addItem
        /// </summary>
        private void addItems(string[] fileNames)
        {
            foreach (string fileName in fileNames)
            {
                addItem(fileName);
            }
        }
        
        /// <summary>
        /// 增加文件
        /// </summary>
        /// <param name="fileName"></param>
        private void addItem(string fileName)
        {
            fileNumber++;

ListViewItem additem = new ListViewItem(fileNumber.ToString());
            additem.SubItems.Add(fileName);
            listView1.Items.Add(additem);
            listView1.Columns[1].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
            //是否显示button1
            buttonVisible();
        }

/// <summary>
        /// 清除所有项
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void clearAll_Click(object sender, EventArgs e)
        {
            listView1.Items.Clear();
            fileNumber = 0;
            buttonVisible();
        }

/// <summary>
        /// 清除选择项
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void deleteSelected_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem item in listView1.SelectedItems)
            {
                item.Remove();
            }
            buttonVisible();
        }

/// <summary>
        /// 窗口移动,触发moveHaveRaise事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form2_Move(object sender, EventArgs e)
        {
            if (moveHaveRaise != null)
            {
                moveHaveRaise(this, e);
            }
        }

/// <summary>
        /// 窗口大小改变,触发moveHaveRaise事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form2_ResizeEnd(object sender, EventArgs e)
        {
            if (moveHaveRaise != null)
            {
                moveHaveRaise(this, e);
            }

buttonVisible();
        }

/// <summary>
        /// 选择想改变,触发itemSelectChange事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void listView1_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
        {
            if (itemSelectChange != null)
            {
                itemSelectChange(this,e);
            }
        }

/// <summary>
        /// 属性:返回当前所有选择项
        /// </summary>
        public ListViewItem[] SelectedItemArray
        {
            get
            {
                ListViewItem[] selectedItemNames = new ListViewItem[listView1.SelectedItems.Count];
                listView1.SelectedItems.CopyTo(selectedItemNames, 0);
                return selectedItemNames;
            }
        }

/// <summary>
        /// 属性:用GDI+为选择项画一个红色矩形,调用下面的Flash
        /// </summary>
        public string flashItem
        {
            set
            {
                string flashFile = value;
                Flash(flashFile);
            }
        }

/// <summary>
        /// 用GDI+为选择项画一个红色矩形,调用下面的drawItem
        /// </summary>
        /// <param name="flashFile"></param>
        private void Flash(string flashFile)
        {
            listView1.Refresh();

ListViewItem foundItem = listView1.FindItemWithText(flashFile, true, listView1.SelectedIndices[0]);

drawItem(foundItem);
        }

/// <summary>
        /// 用GDI+为选择项画一个红色矩形
        /// </summary>
        /// <param name="foundItem"></param>
        private void drawItem(ListViewItem currentItem)
        {
            if (currentItem != null)
            {
                //创建画笔颜色
                Pen redPen = new Pen(Color.Red);                
                //创建绘画图面
                Graphics itemGraphics = listView1.CreateGraphics();
                //创建矩形
                Rectangle rect = currentItem.Bounds;
                currentItem.Bounds.Intersect(rect);
                //在屏幕上画矩形
                itemGraphics.DrawRectangle(redPen, rect);
                //释放资源
                redPen.Dispose();
                itemGraphics.Dispose();
            }
        }

/// <summary>
        /// 窗口关闭前,将主窗口textBox1清空
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form2_FormClosing(object sender, FormClosingEventArgs e)
        {
            listView1.Items.Clear();
            if (itemSelectChange != null)
            {
                itemSelectChange(this, e);
            }
        }

/// <summary>
        /// 将窗口宽度在listView1实际宽度和主窗口宽度之间切换
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            int width1=listView1.Columns[0].Width + listView1.Columns[1].Width;

if (width1 > this.Width)
            {
                this.Width = width1 + 30;
            }
            else
            {
                this.Width = 348;
            }

button1.Left = this.Width - 27;
            listView1.Select();
        }

/// <summary>
        /// 根据窗体宽度和listView1实际宽度,设置button1是否可见
        /// </summary>
        private void buttonVisible()
        {
            int width1 = listView1.Columns[0].Width + listView1.Columns[1].Width + 30;

if (width1 > this.Width)
            {
                button1.Visible = true;
            }
            else
            {
                button1.Visible = false;
            }

button1.Left = this.Width - 27;
        }
    }
}

文件属性修改器(C#)相关推荐

  1. MongoDB update修改器: 针对Fields的$修改器 $inc $set $unset

    MongoDB update修改器: $inc $set $unset $push $pull $pop 针对Fields的$修改器 $set: { $set: { key: value } } $s ...

  2. MongoDB修改器的使用1

    为什么要使用修改器?     通常我们只会修改文档的一部分,这时候更新整个文档就显得很麻烦,通常是通过原子性的更新修改器来完成. 1."$set"修改器    "$set ...

  3. thinkphp模型中的获取器和修改器(根据字段名自动调用模型中的方法)

    thinkphp模型中的获取器和修改器(根据字段名自动调用模型中的方法) 一.总结 记得看下面 1.获取器的作用是在获取数据的字段值后自动进行处理 2.修改器的作用是可以在数据赋值的时候自动进行转换处 ...

  4. 【v2.x OGE-example 第二章(第二节) 修改器的使用】

    2019独角兽企业重金招聘Python工程师标准>>> [v2.x OGE-example 第二章(第二节) 修改器的使用] 1. 位置:Modifier_example --> ...

  5. 利用cheat engine以及VC编写游戏修改器

    cheat engine的介绍已经完毕了,下面就是怎么使用它的问题,这里写一个稍微有意思一点的,也有实际用途的话题,就是来编写自己的游戏修改器. 这篇文章参考了http://www.pediy.com ...

  6. IP修改器的作用以及用途

    现在网络无处不在,有一些网站会对用户有一定的限制,比如不能频繁发帖,不能一直频繁访问,那么遇到这些问题怎能办呢? 每一台连接了互联网的电脑都有一个唯一的IP地址如同我们每一个人的×××号码一样,那么这 ...

  7. mongodb 学习笔记 2 --- 修改器

    修改器是为了爱update文档时,不需要传入整个文档就能修改当前文档的某个属性值,修改器用法如下: 假设数据库中foo集合中存在如下文档:{"name":"jack&qu ...

  8. MongoDB(课时18 修改器)

    3.4.3.2 修改器(原子操作) 对MongoDB数据库而言,数据的修改会牵扯到内容的变更,结构的变更(包含数组),所以在MongoDB在设计的时候就提供有一系列的修改器的应用,那么像之前使用的&q ...

  9. 红色警戒2修改器原理百科(十)

    (二十一)转换阵营--很炫酷却很简单 这个功能,貌似当初很火很轰动,感觉很强大--联网对战中控制对手单位,卖掉对手建筑!右边的建造选项也是对手的,你可以替对手建造单位.可是这个原理很简单,游戏中一个很 ...

最新文章

  1. oracle开发常用,oracle开发常用关键字
  2. Android自定义属性
  3. 【HNOI2007】紧急疏散
  4. mysql5.6编译_mysql5.6编译安装
  5. python apache配置_Apache运行Python的配置
  6. 像@Transactional一样利用注解自定义aop切片
  7. 微信小程序视频弹幕效果
  8. node工程默认url_node命令行工具之实现项目工程自动初始化的标准流程
  9. 假如程序员面试都说真话
  10. 加减法叫做什么运算_【课堂实录】加减法运算的本质(四上)
  11. Android--获取App应用程序的大小
  12. 代码生成利器-NCodeGenerate 教程(2) NCodeGenerate的代码公用之一
  13. 加入了开源组织datawhale
  14. 给 datepicker 设定日期格式
  15. leetcode - Recover Binary Search Tree
  16. NOIP2016普及组复赛 解题分析
  17. python小白系列1
  18. SparkSteaming细节问题
  19. 一文快速了解EL表达式基础知识
  20. 奶制品生产与销售matlab,奶制品的生产与加工

热门文章

  1. 1968年图灵奖得主 – 理查德·卫斯里·汉明 (Richard Hamming)
  2. android免费商用图标,免费商用!!!(线性图标)
  3. QT基础入门【调试篇】QT程序如何打包发布生成可执行exe文件(win下的可执行程序)
  4. 物联网串口在线更新语音的mp3语音芯片ic方案说明
  5. Adobe Lightroom Classic CC2018v7.5新版导入预设方法
  6. 计算机专业能力提升,计算机专业学生能力提升的探索.pdf
  7. 大白话理解局域网以及局域网ip分配(有效引导篇)
  8. adb 卸载 Anroid 内置应用
  9. 植物大战僵尸变态辅助开发系列教程(E语言实现和VC6实现)(下)
  10. 每天一道js基础面试题