和谐UAC与应用程序配置,抛砖引玉,希望完美解决问题。

UAC 是 Vista 与 Win7 安全基础。

 

在打开UAC的情况下,窗体应用程序默认配置信息的读取与写入,在 Program Files 文件夹下将会引发一些错误。

 

研究了很多园子里的朋友,多是通过提升应用程序权限,要求用户以管理员身份使用软件的方式,来解决这个问题。

 

提升权限来回避UAC的控制,这种解决方式,并不妥当。

 

研究了几天,转了好大一圈,终于初步解决了这个问题,半夜三更,担心解决的彻底,在此抛砖引玉,希望完美解决问题。

 

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

 

代码

        
public 构造函数()
        {
            ExeConfigurationFileMap tmp 
= new ExeConfigurationFileMap();

            tmp.ExeConfigFilename 
= ConfigerFile();

            ShiQiangConfiger 
= ConfigurationManager.OpenMappedExeConfiguration(tmp, ConfigurationUserLevel.None);
        }

        
//-------------------------------------------------------------------//

        
private  string ConfigerFile()
        {
                
string rtnValue = string.Empty;

                
int targetIndex = Application.ExecutablePath.LastIndexOf(@"\");

                
if (targetIndex >= 0)
                {
                    
if (!System.IO.Directory.Exists(Application.CommonAppDataPath))
                    {
                        System.IO.Directory.CreateDirectory(Application.CommonAppDataPath);
                    }

                    rtnValue 
= System.IO.Path.Combine(Application.CommonAppDataPath, Application.ExecutablePath.Substring(targetIndex + 1+ ".Config");

                    
if (!System.IO.File.Exists(rtnValue))
                    {
                        
//如果没文件的话,复制app.config,在CommonAppDataPath处创建个新的。
                        System.Configuration.Configuration tmp = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

                        tmp.SaveAs(rtnValue);
                    }
                }

                
return rtnValue;
        }

        
private System.Configuration.Configuration myConfiguration;

 

 下面用 myConfiguration 就可以进行通常的配置文件操作了。

 

posted @ 2010-05-05 01:10  置身珠海,学习与奋斗  阅读(611)  评论(2编辑  收藏  举报