工作空间文件 工程文件 设计
一般我们在软件设计开发中都需要设计自己的工作空间 或者叫做工程文件(gis行业软件大多都是这样的),这种文件的好处当然就是,这次工作没有做完,但是可以保存其信息与工作空间文件中,下次直接打开,很是便利。
前两天作了一个,总结如下:
1、工作空间文件以二进制的形势或者文本形势存储工作中打开的文件, c#中stream方式随便写,不再详述。
2、工作空间文件与应用程序关联(为了双击打开应用程序);通过注册表 代码如下:
         
            
 using System;
using System;
 using System.Collections.Generic;
using System.Collections.Generic;
 using System.Text;
using System.Text;
 using Microsoft.Win32;
using Microsoft.Win32;

 namespace filereg
namespace filereg
 {
{
 public class FileTypeRegInfo
  public class FileTypeRegInfo
 {
    {
 /// <summary>
        /// <summary>
 /// 目标类型文件的扩展名
        /// 目标类型文件的扩展名
 /// </summary>
        /// </summary>
 public string ExtendName ;  //".xcf"
        public string ExtendName ;  //".xcf"
 
        
 /// <summary>
        /// <summary>
 /// 目标文件类型说明
        /// 目标文件类型说明
 /// </summary>
        /// </summary>
 public string Description ; //"XCodeFactory项目文件"
        public string Description ; //"XCodeFactory项目文件"

 /// <summary>
        /// <summary>
 /// 目标类型文件关联的图标
        /// 目标类型文件关联的图标
 /// </summary>
        /// </summary>
 public string IcoPath ;
        public string IcoPath ;

 /// <summary>
        /// <summary>
 /// 打开目标类型文件的应用程序
        /// 打开目标类型文件的应用程序
 /// </summary>
        /// </summary>
 public string ExePath ;
        public string ExePath ;

 public FileTypeRegInfo()
        public FileTypeRegInfo()
 {
        {
 }
        }

 public FileTypeRegInfo(string extendName,string description,string icopath,string exepath)
        public FileTypeRegInfo(string extendName,string description,string icopath,string exepath)
 {
        {
 this.ExtendName = extendName ;
            this.ExtendName = extendName ;
 this.Description = description;
            this.Description = description;
 this.IcoPath = icopath;
            this.IcoPath = icopath;
 this.ExePath = exepath;
            this.ExePath = exepath;
 }
        }
 }   //新注册文件信息
    }   //新注册文件信息

 public class FileTypeRegister
  public class FileTypeRegister
 {
    {        
 public static void RegisterFileType(FileTypeRegInfo regInfo)
        public static void RegisterFileType(FileTypeRegInfo regInfo)
 {
        {
 if(FileTypeRegistered(regInfo.ExtendName))
            if(FileTypeRegistered(regInfo.ExtendName))
 {
            {
 return ;
                return ;
 }
            }
 
            
 string relationName = regInfo.ExtendName.Substring(1 ,regInfo.ExtendName.Length-1).ToUpper() + "_FileType" ;
            string relationName = regInfo.ExtendName.Substring(1 ,regInfo.ExtendName.Length-1).ToUpper() + "_FileType" ;

 RegistryKey fileTypeKey = Registry.ClassesRoot.CreateSubKey(regInfo.ExtendName) ;
            RegistryKey fileTypeKey = Registry.ClassesRoot.CreateSubKey(regInfo.ExtendName) ;
 fileTypeKey.SetValue("" ,relationName) ;
            fileTypeKey.SetValue("" ,relationName) ;
 fileTypeKey.Close() ;
            fileTypeKey.Close() ;
 
            
 RegistryKey relationKey = Registry.ClassesRoot.CreateSubKey(relationName) ;
            RegistryKey relationKey = Registry.ClassesRoot.CreateSubKey(relationName) ;
 relationKey.SetValue("" ,regInfo.Description) ;
            relationKey.SetValue("" ,regInfo.Description) ;

 RegistryKey iconKey = relationKey.CreateSubKey("DefaultIcon") ;
            RegistryKey iconKey = relationKey.CreateSubKey("DefaultIcon") ;
 iconKey.SetValue("" ,regInfo.IcoPath) ;
            iconKey.SetValue("" ,regInfo.IcoPath) ;

 RegistryKey shellKey   = relationKey.CreateSubKey("Shell") ;
            RegistryKey shellKey   = relationKey.CreateSubKey("Shell") ;
 RegistryKey openKey    = shellKey.CreateSubKey("Open") ;
            RegistryKey openKey    = shellKey.CreateSubKey("Open") ;
 RegistryKey commandKey = openKey.CreateSubKey("Command") ;
            RegistryKey commandKey = openKey.CreateSubKey("Command") ;
 commandKey.SetValue("" ,regInfo.ExePath + " %1") ;
            commandKey.SetValue("" ,regInfo.ExePath + " %1") ;        
 
            
 relationKey.Close() ;
            relationKey.Close() ;
 }//注册新文件类型
        }//注册新文件类型
 
   
 public static FileTypeRegInfo GetFileTypeRegInfo(string extendName)
        public static FileTypeRegInfo GetFileTypeRegInfo(string extendName)
 {
        {
 if(!FileTypeRegistered(extendName))
            if(!FileTypeRegistered(extendName))
 {
            {
 return null ;
                return null ;
 }
            }

 FileTypeRegInfo regInfo = new FileTypeRegInfo();
            FileTypeRegInfo regInfo = new FileTypeRegInfo();

 string relationName     = extendName.Substring(1 ,extendName.Length-1).ToUpper() + "_FileType" ;
            string relationName     = extendName.Substring(1 ,extendName.Length-1).ToUpper() + "_FileType" ;
 RegistryKey relationKey = Registry.ClassesRoot.OpenSubKey(relationName) ;
            RegistryKey relationKey = Registry.ClassesRoot.OpenSubKey(relationName) ;
 regInfo.Description     = relationKey.GetValue("").ToString() ;
            regInfo.Description     = relationKey.GetValue("").ToString() ;

 RegistryKey iconKey = relationKey.OpenSubKey("DefaultIcon") ;
            RegistryKey iconKey = relationKey.OpenSubKey("DefaultIcon") ;
 regInfo.IcoPath     = iconKey.GetValue("").ToString();
            regInfo.IcoPath     = iconKey.GetValue("").ToString();

 RegistryKey shellKey   = relationKey.OpenSubKey("Shell") ;
            RegistryKey shellKey   = relationKey.OpenSubKey("Shell") ;
 RegistryKey openKey    = shellKey.OpenSubKey("Open") ;
            RegistryKey openKey    = shellKey.OpenSubKey("Open") ;
 RegistryKey commandKey = openKey.OpenSubKey("Command") ;
            RegistryKey commandKey = openKey.OpenSubKey("Command") ;
 string temp            = commandKey.GetValue("").ToString() ;
            string temp            = commandKey.GetValue("").ToString() ;    
 regInfo.ExePath           = temp.Substring(0 ,temp.Length-3) ;
            regInfo.ExePath           = temp.Substring(0 ,temp.Length-3) ;  
 
   
 return regInfo ;
            return regInfo ;
 }//察看注册信息
        }//察看注册信息

 /// <summary>
        /// <summary>
 /// UpdateFileTypeRegInfo 更新指定文件类型关联信息
        /// UpdateFileTypeRegInfo 更新指定文件类型关联信息
 /// </summary>
        /// </summary>    
 public static bool UpdateFileTypeRegInfo(FileTypeRegInfo regInfo)
        public static bool UpdateFileTypeRegInfo(FileTypeRegInfo regInfo)
 {
        {
 if(!FileTypeRegistered(regInfo.ExtendName))
            if(!FileTypeRegistered(regInfo.ExtendName))
 {
            {
 return false ;
                return false ;
 }
            }   
 string extendName       = regInfo.ExtendName ;
            string extendName       = regInfo.ExtendName ;
 string relationName     = extendName.Substring(1 ,extendName.Length-1).ToUpper() + "_FileType" ;
            string relationName     = extendName.Substring(1 ,extendName.Length-1).ToUpper() + "_FileType" ;
 RegistryKey relationKey = Registry.ClassesRoot.OpenSubKey(relationName ,true) ;
            RegistryKey relationKey = Registry.ClassesRoot.OpenSubKey(relationName ,true) ;
 relationKey.SetValue("" ,regInfo.Description) ;
            relationKey.SetValue("" ,regInfo.Description) ;

 RegistryKey iconKey = relationKey.OpenSubKey("DefaultIcon" ,true) ;
            RegistryKey iconKey = relationKey.OpenSubKey("DefaultIcon" ,true) ;
 iconKey.SetValue("" ,regInfo.IcoPath);
            iconKey.SetValue("" ,regInfo.IcoPath);

 RegistryKey shellKey   = relationKey.OpenSubKey("Shell") ;
            RegistryKey shellKey   = relationKey.OpenSubKey("Shell") ;
 RegistryKey openKey    = shellKey.OpenSubKey("Open") ;
            RegistryKey openKey    = shellKey.OpenSubKey("Open") ;
 RegistryKey commandKey = openKey.OpenSubKey("Command" ,true) ;
            RegistryKey commandKey = openKey.OpenSubKey("Command" ,true) ;
 commandKey.SetValue("" ,regInfo.ExePath + " %1") ;
            commandKey.SetValue("" ,regInfo.ExePath + " %1") ;    

 relationKey.Close();
            relationKey.Close();
 return true ;
            return true ;
 }//更新注册信息
        }//更新注册信息

 /// <summary>
        /// <summary>
 /// FileTypeRegistered 指定文件类型是否已经注册
        /// FileTypeRegistered 指定文件类型是否已经注册
 /// </summary>
        /// </summary>        
 public static bool FileTypeRegistered(string extendName)
        public static bool FileTypeRegistered(string extendName)
 {
        {
 RegistryKey softwareKey = Registry.ClassesRoot.OpenSubKey(extendName);
            RegistryKey softwareKey = Registry.ClassesRoot.OpenSubKey(extendName);
 if(softwareKey != null)
            if(softwareKey != null)
 {
            {
 return true ;
                return true ;
 }
            }

 return false ;
            return false ;
 }//判断是否注册
        }//判断是否注册
 
       
 }
    }
 }
}
          
3、双击应用程序打开,应用程序读取工作空间中存储的信息(例如:另一个文件的路径)
        
System.Environment.GetCommandLineArgs() 此方法可获取工作空间文件路径,返回值是一个string[] ,[0]为应用程序路径,[1]为工作空间路径。 注意:此string[] 是被传递进来的参数中空格分开形成,祥请参考:msdn
一般我们在软件设计开发中都需要设计自己的工作空间 或者叫做工程文件(gis行业软件大多都是这样的),这种文件的好处当然就是,这次工作没有做完,但是可以保存其信息与工作空间文件中,下次直接打开,很是便利。
前两天作了一个,总结如下:
1、工作空间文件以二进制的形势或者文本形势存储工作中打开的文件, c#中stream方式随便写,不再详述。
2、工作空间文件与应用程序关联(为了双击打开应用程序);通过注册表 代码如下:
 using System;
using System; using System.Collections.Generic;
using System.Collections.Generic; using System.Text;
using System.Text; using Microsoft.Win32;
using Microsoft.Win32;
 namespace filereg
namespace filereg {
{ public class FileTypeRegInfo
  public class FileTypeRegInfo {
    { /// <summary>
        /// <summary> /// 目标类型文件的扩展名
        /// 目标类型文件的扩展名 /// </summary>
        /// </summary> public string ExtendName ;  //".xcf"
        public string ExtendName ;  //".xcf" 
         /// <summary>
        /// <summary> /// 目标文件类型说明
        /// 目标文件类型说明 /// </summary>
        /// </summary> public string Description ; //"XCodeFactory项目文件"
        public string Description ; //"XCodeFactory项目文件"
 /// <summary>
        /// <summary> /// 目标类型文件关联的图标
        /// 目标类型文件关联的图标 /// </summary>
        /// </summary> public string IcoPath ;
        public string IcoPath ;
 /// <summary>
        /// <summary> /// 打开目标类型文件的应用程序
        /// 打开目标类型文件的应用程序 /// </summary>
        /// </summary> public string ExePath ;
        public string ExePath ;
 public FileTypeRegInfo()
        public FileTypeRegInfo() {
        { }
        }
 public FileTypeRegInfo(string extendName,string description,string icopath,string exepath)
        public FileTypeRegInfo(string extendName,string description,string icopath,string exepath) {
        { this.ExtendName = extendName ;
            this.ExtendName = extendName ; this.Description = description;
            this.Description = description; this.IcoPath = icopath;
            this.IcoPath = icopath; this.ExePath = exepath;
            this.ExePath = exepath; }
        } }   //新注册文件信息
    }   //新注册文件信息
 public class FileTypeRegister
  public class FileTypeRegister {
    {         public static void RegisterFileType(FileTypeRegInfo regInfo)
        public static void RegisterFileType(FileTypeRegInfo regInfo) {
        { if(FileTypeRegistered(regInfo.ExtendName))
            if(FileTypeRegistered(regInfo.ExtendName)) {
            { return ;
                return ; }
            } 
             string relationName = regInfo.ExtendName.Substring(1 ,regInfo.ExtendName.Length-1).ToUpper() + "_FileType" ;
            string relationName = regInfo.ExtendName.Substring(1 ,regInfo.ExtendName.Length-1).ToUpper() + "_FileType" ;
 RegistryKey fileTypeKey = Registry.ClassesRoot.CreateSubKey(regInfo.ExtendName) ;
            RegistryKey fileTypeKey = Registry.ClassesRoot.CreateSubKey(regInfo.ExtendName) ; fileTypeKey.SetValue("" ,relationName) ;
            fileTypeKey.SetValue("" ,relationName) ; fileTypeKey.Close() ;
            fileTypeKey.Close() ; 
             RegistryKey relationKey = Registry.ClassesRoot.CreateSubKey(relationName) ;
            RegistryKey relationKey = Registry.ClassesRoot.CreateSubKey(relationName) ; relationKey.SetValue("" ,regInfo.Description) ;
            relationKey.SetValue("" ,regInfo.Description) ;
 RegistryKey iconKey = relationKey.CreateSubKey("DefaultIcon") ;
            RegistryKey iconKey = relationKey.CreateSubKey("DefaultIcon") ; iconKey.SetValue("" ,regInfo.IcoPath) ;
            iconKey.SetValue("" ,regInfo.IcoPath) ;
 RegistryKey shellKey   = relationKey.CreateSubKey("Shell") ;
            RegistryKey shellKey   = relationKey.CreateSubKey("Shell") ; RegistryKey openKey    = shellKey.CreateSubKey("Open") ;
            RegistryKey openKey    = shellKey.CreateSubKey("Open") ; RegistryKey commandKey = openKey.CreateSubKey("Command") ;
            RegistryKey commandKey = openKey.CreateSubKey("Command") ; commandKey.SetValue("" ,regInfo.ExePath + " %1") ;
            commandKey.SetValue("" ,regInfo.ExePath + " %1") ;         
             relationKey.Close() ;
            relationKey.Close() ; }//注册新文件类型
        }//注册新文件类型 
    public static FileTypeRegInfo GetFileTypeRegInfo(string extendName)
        public static FileTypeRegInfo GetFileTypeRegInfo(string extendName) {
        { if(!FileTypeRegistered(extendName))
            if(!FileTypeRegistered(extendName)) {
            { return null ;
                return null ; }
            }
 FileTypeRegInfo regInfo = new FileTypeRegInfo();
            FileTypeRegInfo regInfo = new FileTypeRegInfo();
 string relationName     = extendName.Substring(1 ,extendName.Length-1).ToUpper() + "_FileType" ;
            string relationName     = extendName.Substring(1 ,extendName.Length-1).ToUpper() + "_FileType" ; RegistryKey relationKey = Registry.ClassesRoot.OpenSubKey(relationName) ;
            RegistryKey relationKey = Registry.ClassesRoot.OpenSubKey(relationName) ; regInfo.Description     = relationKey.GetValue("").ToString() ;
            regInfo.Description     = relationKey.GetValue("").ToString() ;
 RegistryKey iconKey = relationKey.OpenSubKey("DefaultIcon") ;
            RegistryKey iconKey = relationKey.OpenSubKey("DefaultIcon") ; regInfo.IcoPath     = iconKey.GetValue("").ToString();
            regInfo.IcoPath     = iconKey.GetValue("").ToString();
 RegistryKey shellKey   = relationKey.OpenSubKey("Shell") ;
            RegistryKey shellKey   = relationKey.OpenSubKey("Shell") ; RegistryKey openKey    = shellKey.OpenSubKey("Open") ;
            RegistryKey openKey    = shellKey.OpenSubKey("Open") ; RegistryKey commandKey = openKey.OpenSubKey("Command") ;
            RegistryKey commandKey = openKey.OpenSubKey("Command") ; string temp            = commandKey.GetValue("").ToString() ;
            string temp            = commandKey.GetValue("").ToString() ;     regInfo.ExePath           = temp.Substring(0 ,temp.Length-3) ;
            regInfo.ExePath           = temp.Substring(0 ,temp.Length-3) ;   
    return regInfo ;
            return regInfo ; }//察看注册信息
        }//察看注册信息
 /// <summary>
        /// <summary> /// UpdateFileTypeRegInfo 更新指定文件类型关联信息
        /// UpdateFileTypeRegInfo 更新指定文件类型关联信息 /// </summary>
        /// </summary>     public static bool UpdateFileTypeRegInfo(FileTypeRegInfo regInfo)
        public static bool UpdateFileTypeRegInfo(FileTypeRegInfo regInfo) {
        { if(!FileTypeRegistered(regInfo.ExtendName))
            if(!FileTypeRegistered(regInfo.ExtendName)) {
            { return false ;
                return false ; }
            }    string extendName       = regInfo.ExtendName ;
            string extendName       = regInfo.ExtendName ; string relationName     = extendName.Substring(1 ,extendName.Length-1).ToUpper() + "_FileType" ;
            string relationName     = extendName.Substring(1 ,extendName.Length-1).ToUpper() + "_FileType" ; RegistryKey relationKey = Registry.ClassesRoot.OpenSubKey(relationName ,true) ;
            RegistryKey relationKey = Registry.ClassesRoot.OpenSubKey(relationName ,true) ; relationKey.SetValue("" ,regInfo.Description) ;
            relationKey.SetValue("" ,regInfo.Description) ;
 RegistryKey iconKey = relationKey.OpenSubKey("DefaultIcon" ,true) ;
            RegistryKey iconKey = relationKey.OpenSubKey("DefaultIcon" ,true) ; iconKey.SetValue("" ,regInfo.IcoPath);
            iconKey.SetValue("" ,regInfo.IcoPath);
 RegistryKey shellKey   = relationKey.OpenSubKey("Shell") ;
            RegistryKey shellKey   = relationKey.OpenSubKey("Shell") ; RegistryKey openKey    = shellKey.OpenSubKey("Open") ;
            RegistryKey openKey    = shellKey.OpenSubKey("Open") ; RegistryKey commandKey = openKey.OpenSubKey("Command" ,true) ;
            RegistryKey commandKey = openKey.OpenSubKey("Command" ,true) ; commandKey.SetValue("" ,regInfo.ExePath + " %1") ;
            commandKey.SetValue("" ,regInfo.ExePath + " %1") ;    
 relationKey.Close();
            relationKey.Close(); return true ;
            return true ; }//更新注册信息
        }//更新注册信息
 /// <summary>
        /// <summary> /// FileTypeRegistered 指定文件类型是否已经注册
        /// FileTypeRegistered 指定文件类型是否已经注册 /// </summary>
        /// </summary>         public static bool FileTypeRegistered(string extendName)
        public static bool FileTypeRegistered(string extendName) {
        { RegistryKey softwareKey = Registry.ClassesRoot.OpenSubKey(extendName);
            RegistryKey softwareKey = Registry.ClassesRoot.OpenSubKey(extendName); if(softwareKey != null)
            if(softwareKey != null) {
            { return true ;
                return true ; }
            }
 return false ;
            return false ; }//判断是否注册
        }//判断是否注册 
        }
    } }
}3、双击应用程序打开,应用程序读取工作空间中存储的信息(例如:另一个文件的路径)
System.Environment.GetCommandLineArgs() 此方法可获取工作空间文件路径,返回值是一个string[] ,[0]为应用程序路径,[1]为工作空间路径。 注意:此string[] 是被传递进来的参数中空格分开形成,祥请参考:msdn
 
                    
                 


 
     
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号