• C# .NET4.0 自定义文件并实现文件与应用程序关联


     

    一、如何创建自定义后缀名的文件(如 *.tmp)?

     

        使用序列化将一个对象序列化成文件,使用 BinaryFormatter 类提供方法可以将文件序列 

     

      化为二进制文件存放。

        注意:待序列化的类必须标示可序列化,方法是在 public class Test{}上方加标签      

     

              [System.Serializable]

        

    通过上一步我们已经知道创建一个自定义扩展名文件的思路了,下面是具体的代码,包括两部分

    一个是如可保存文件,一个是如可加载文件(这个类文件附在程序里就能看效果了!!)

     

    ------------------------------------------------------------------------

    using System.IO;

    using System.Xml.Serialization;

    using System.Runtime.Serialization.Formatters.Binary;

    using System.Runtime.Serialization;

     

    namespace SerializerTeset.CommonClass

    {

        [System.Serializable]

        public class TestForBinary : ISerializerObject

        {

            private string m_name = string.Empty;

            private string m_userid = string.Empty;

     

            public string UserName

            {

                set { m_name = value; }

                get { return m_name; }

            }

     

            public string UserId

            {

                set { m_userid = value; }

                get { return m_userid; }

            }

     

            /// <summary>

            /// 保存序列化对象

            /// </summary>

            /// <param name="fileName">文件完整路径包括名称(c:/test.xxj</param>

            /// <returns></returns>

            public bool Save(string fileName)

            {

                BinaryFormatter bf = new BinaryFormatter();

                using (FileStream fs=new FileStream(fileName,FileMode.Create))

                {

                    bf.Serialize(fs, this);

                    fs.Close();

                }

     

                return true;

            }

     

            /// <summary>

            /// 反序列化以保存的对象

            /// </summary>

            /// <param name="fileName">文件完整路径包括名称(c:/test.tmp</param>

            /// <returns></returns>

            public bool Load(string fileName)

            {

                BinaryFormatter xs = new BinaryFormatter();

                TestForBinary tc;

                using (FileStream fs = new FileStream(fileName, FileMode.Open))

                {

                    tc = xs.Deserialize(fs) as TestForBinary;

                }

     

                if (tc == null)

                    return false;

     

                this.m_name = tc.UserName;

                this.UserId = tc.UserId;

     

                return true;

            }

     

        }

    }

    ------------------------------------------------------------------------

     

    怎么样,是不是很有意思啊?不过问题又来了:“如何双击这个自定义后缀的文件就能打开咱们

    的应用程序呢?”

     

    二、如何关联文件类型和应用程序

     

    其实这个功能需要 windows 来帮助了,就是需要修改注册表了,打开注册表编辑器(开始-》运

     

    行,输入 regedit 后回车),我们需要在注册表的 HKEY_CLASSES_ROOT 节点下加点东西就 OK

     

    了。

     

    -- 自己可以手写

    HKEY_CLASSES_ROOT

          |

           -.xxj = MyApp     

          |

           - MyApp = My Application

             |

             -- Shell

                   |

                   -- open

                         |

                         -- command = c:/mydir/my.exe %1

     

    注释:.xxj 就是你自己想定义的后缀名了

          MyApp 节点名也是自定义的,注意两处 MyApp 要保持一致哦!

     

     

    -- 当然也可以用代码搞定它了!

    ------------------------------------------------------------------------

            private bool SetFileExtend()

            {

                try

                {

                    string appPath = Application.ExecutablePath;

                    string extendName = ".xxj"; //自定义的文件后缀名

                    string extendDefaultSet = "XXJ";

                    string directions = "草青工作室";

     

                    if (Registry.ClassesRoot.OpenSubKey(extendName) != null)

                    {

                        MessageBox.Show("关联已经创建!");

                        return false;

                    }

     

                    RegistryKey key;

                    //1 设置自定义文件的双击打开

                    key = Registry.ClassesRoot.CreateSubKey(extendDefaultSet);

                    key.SetValue("", directions);

                    key = key.CreateSubKey("shell");

                    key = key.CreateSubKey("open");

                    key = key.CreateSubKey("command");

                    key.SetValue("", string.Format("{0} %1", appPath));

     

                    //2 设置自定义文件的默认图标  --这点测试时有点问题,还有待研究

                    key = Registry.ClassesRoot.OpenSubKey(extendDefaultSet,true);

                    key = key.CreateSubKey("DefaultIcon");

                    key.SetValue("", string.Format("{0},0", appPath));

     

                    //3 新增的扩展名和设置关联

                    key = Registry.ClassesRoot.CreateSubKey(extendName);

                    key.SetValue("", extendDefaultSet);

     

     

                    MessageBox.Show("关联创建成功!");

                    return true;

                }

                catch (Exception ex)

                {

                    MessageBox.Show(ex.Message);

                    return false;

                }

            }

    ------------------------------------------------------------------------

     

    真是太有意思了,现在系统中有了一个自己定义的文件了!!!!,不过问题又来了“

    有了类型,双击文件也能打开自己的应用程序了,但是如何在打开程序后加载内容呢?”

     

     

    三、双击一个自定后缀的文件,打开应用程序后如可加载其中的内容呢?

     

    其实这个需要解决两个问题:1.如何获得双击文件的完整路径。2.如何加载内容

    对于第一点那就要使用“命令行参数”了,还记得这个词吧?不记得的自己翻书去.......

    哈哈,不买官司了!命令行参数就是主程序入口那个 main 方法的形参啊,如果没有可以

    自己加一个,这个形参是 string[] 的数组,数组的第一位就是你要的东东了!

    ------------------------------------------------------------------------

    [STAThread]

    static void Main(string[] args)

    {

        if(args.Length > 0)

            MessageBox.Show("文件路径: " + args[0]).ToString());

            Application.Run(new FrmMain(args[0]));

            //args[0] 就是双击打开的文件名(包含文件路径)

        else

            Application.Run(new FrmMain(null));

    }

    ------------------------------------------------------------------------

     

    到这里我们就有了一个比较健全的自定义后缀名的文件了!原代码文件在我的下载中有,有兴趣的可以看看!

     

     

     

    本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/xxj_jing/archive/2010/06/29/5702962.aspx

  • 相关阅读:
    Python+Selenium笔记(十二):数据驱动测试
    Python+Selenium笔记(十一):配置selenium Grid
    Python+Selenium笔记(十):元素等待机制
    Python+Selenium笔记(九):操作警告和弹出框
    Python+Selenium笔记(八):操作下拉菜单
    Python+Selenium笔记(七):WebDriver和WebElement
    Python+Selenium笔记(六):元素定位
    Python+Selenium笔记(五):生成测试报告
    Python+Selenium笔记(四):unittest的Test Suite(测试套件)
    Python+Selenium笔记(三):使用unittest
  • 原文地址:https://www.cnblogs.com/yja9010/p/3178820.html
Copyright © 2020-2023  润新知