工作空间文件 工程文件 设计


         一般我们在软件设计开发中都需要设计自己的工作空间 或者叫做工程文件(gis行业软件大多都是这样的),这种文件的好处当然就是,这次工作没有做完,但是可以保存其信息与工作空间文件中,下次直接打开,很是便利。

        前两天作了一个,总结如下:

         1、工作空间文件以二进制的形势或者文本形势存储工作中打开的文件, c#中stream方式随便写,不再详述。
         2、工作空间文件与应用程序关联(为了双击打开应用程序);通过注册表 代码如下:
         
            
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Win32;

namespace filereg
{
  
public class FileTypeRegInfo
    
{
        
/// <summary>
        
/// 目标类型文件的扩展名
        
/// </summary>

        public string ExtendName ;  //".xcf"
        
        
/// <summary>
        
/// 目标文件类型说明
        
/// </summary>

        public string Description ; //"XCodeFactory项目文件"

        
/// <summary>
        
/// 目标类型文件关联的图标
        
/// </summary>

        public string IcoPath ;

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

        public string ExePath ;

        
public FileTypeRegInfo()
        
{
        }


        
public FileTypeRegInfo(string extendName,string description,string icopath,string exepath)
        
{
            
this.ExtendName = extendName ;
            
this.Description = description;
            
this.IcoPath = icopath;
            
this.ExePath = exepath;
        }

    }
   //新注册文件信息

  
public class FileTypeRegister
    
{        
        
public static void RegisterFileType(FileTypeRegInfo regInfo)
        
{
            
if(FileTypeRegistered(regInfo.ExtendName))
            
{
                
return ;
            }

            
            
string relationName = regInfo.ExtendName.Substring(1 ,regInfo.ExtendName.Length-1).ToUpper() + "_FileType" ;

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

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

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


            FileTypeRegInfo regInfo 
= new FileTypeRegInfo();

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

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

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

        
/// <summary>
        
/// UpdateFileTypeRegInfo 更新指定文件类型关联信息
        
/// </summary>    

        public static bool UpdateFileTypeRegInfo(FileTypeRegInfo regInfo)
        
{
            
if(!FileTypeRegistered(regInfo.ExtendName))
            
{
                
return false ;
            }
   
            
string extendName       = regInfo.ExtendName ;
            
string relationName     = extendName.Substring(1 ,extendName.Length-1).ToUpper() + "_FileType" ;
            RegistryKey relationKey 
= Registry.ClassesRoot.OpenSubKey(relationName ,true) ;
            relationKey.SetValue(
"" ,regInfo.Description) ;

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

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

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

        
/// <summary>
        
/// FileTypeRegistered 指定文件类型是否已经注册
        
/// </summary>        

        public static bool FileTypeRegistered(string extendName)
        
{
            RegistryKey softwareKey 
= Registry.ClassesRoot.OpenSubKey(extendName);
            
if(softwareKey != null)
            
{
                
return true ;
            }


            
return false ;
        }
//判断是否注册
       
    }

}

          
       3、双击应用程序打开,应用程序读取工作空间中存储的信息(例如:另一个文件的路径)
        
         System.Environment.GetCommandLineArgs() 此方法可获取工作空间文件路径,返回值是一个string[] ,[0]为应用程序路径,[1]为工作空间路径。 注意:此string[] 是被传递进来的参数中空格分开形成,祥请参考:msdn
posted on 2007-09-27 16:50  y-xf  阅读(419)  评论(0编辑  收藏  举报