• .net操作系统文件(ini与dat)的帮助类


    .net操作系统文件(ini与dat)的帮助类

    View Code
      1 using System;
      2 using System.Text;
      3 using System.Runtime.InteropServices;
      4 using System.Diagnostics;
      5 
      6 using System.IO;
      7 using System.Data;
      8 
      9 /// <summary>
     10 /// edit by: 201206.14.1535
     11 /// </summary>
     12 public class Ini
     13 {
     14     /// <summary>
     15     /// 16     /// </summary>
     17     /// <param name="section">INI文件中的段落名称</param>
     18     /// <param name="key">INI文件中的关键字</param>
     19     /// <param name="def">无法读取时候时候的缺省数值</param>
     20     /// <param name="retVal">读取数值</param>
     21     /// <param name="size">数值的大小</param>
     22     /// <param name="filePath">INI文件的完整路径和名称</param>
     23     /// <returns></returns>
     24     [DllImport("kernel32")]
     25     private static extern int GetPrivateProfileString(String section, String key, String def, StringBuilder retVal, int size, String filePath);
     26     /// <summary>
     27     /// 28     /// </summary>
     29     /// <param name="section">INI文件中的段落</param>
     30     /// <param name="key">INI文件中的关键字</param>
     31     /// <param name="val">INI文件中关键字的数值</param>
     32     /// <param name="filePath">INI文件的完整的路径和名称</param>
     33     /// <returns></returns>
     34     [DllImport("kernel32")]
     35     private static extern long WritePrivateProfileString(String section, String key, String val, String filePath);
     36 
     37     public Ini() { }
     38 
     39     /// <summary>
     40     /// 构造方法
     41     /// </summary>
     42     /// <param name="INIPath">文件路径</param>
     43     public Ini(string Path)
     44     {
     45         Ini.Path = Path;
     46     }
     47 
     48     #region 类的对象
     49     private static string Path = "";
     50 
     51     /// <summary>
     52     /// 53     /// </summary>
     54     /// <param name="section">项目名称(如 [TypeName] )</param>
     55     /// <param name="key"></param>
     56     /// <returns></returns>
     57     public string get_value(String section, String key)
     58     {
     59         string result = "";
     60         try
     61         {
     62             result = Ini.get_value(section, key, Path);
     63         }
     64         catch (Exception ex)
     65         {
     66         }
     67         return result;
     68     }
     69     /// <summary>
     70     /// 71     /// </summary>
     72     /// <param name="section">项目名称(如 [TypeName] )</param>
     73     /// <param name="key"></param>
     74     /// <param name="val"></param>
     75     /// <returns></returns>
     76     public bool set_value(String section, String key, String val)
     77     {
     78         bool resultFlag = false;
     79         try
     80         {
     81             resultFlag = Ini.set_value(section, key, val, Path);
     82         }
     83         catch (Exception ex)
     84         {
     85         }
     86         return resultFlag;
     87     }
     88 
     89     #endregion
     90 
     91     #region 类 静态
     92     /// <summary>
     93     /// 功能: 获取节点的值
     94     /// </summary>
     95     /// <param name="section">项目名称(如 [TypeName] )</param>
     96     /// <param name="key"></param>
     97     /// <param name="file"></param>
     98     /// <returns></returns>
     99     public static String get_value(String section, String key, string file)
    100     {
    101         StringBuilder builder = new StringBuilder(1024);
    102         try
    103         {
    104             GetPrivateProfileString(section, key, "", builder, 1024, file);
    105             if (builder.ToString() == "")
    106             {
    107                 set_value(section, key, "", file);
    108             }
    109         }
    110         catch (Exception ex)
    111         {
    112         }
    113         return builder.ToString();
    114     }
    115 
    116     /// <summary>
    117     /// 功能: 给节点赋值
    118     /// </summary>
    119     /// <param name="section">项目名称(如 [TypeName] )</param>
    120     /// <param name="key"></param>
    121     /// <param name="val"></param>
    122     /// <param name="file"></param>
    123     /// <returns></returns>
    124     public static bool set_value(String section, String key, String val, string file)
    125     {
    126         try
    127         {
    128             WritePrivateProfileString(section, key, val, file);
    129             return true;
    130         }
    131         catch (Exception ex)
    132         {
    133             return false;
    134         }
    135     }
    136     #endregion
    137 
    138     /// <summary>
    139     /// 创建INI配置文件
    140     /// </summary>
    141     /// <param name="fileName">ini配置文件名称</param>
    142     /// <returns>返回一个状态值 100成功创建 200创建失败</returns>
    143     public static string CreateIni(string fileName)
    144     {
    145         try
    146         {
    147             if (!Directory.Exists(Path))
    148             {
    149                 Directory.CreateDirectory(Path);
    150             }
    151             string filePath = Path + "\\" + fileName;
    152 
    153             if (File.Exists(filePath))
    154             {
    155                 File.Delete(filePath);
    156             }
    157             File.Create(filePath).Close();
    158             return "100";//成功创建
    159         }
    160         catch (Exception ex)
    161         {
    162             //LogHelper.WriteLogError(ex.ToString());
    163             return "200";//创建失败
    164         }
    165     }
    166 
    167     /// <summary>
    168     /// 验证文件是否存在
    169     /// </summary>
    170     /// <returns>布尔值</returns>
    171     public static bool ExistINIFile()
    172     {
    173         return File.Exists(Path);
    174     }
    175 }

    注:需要给文件赋予用户角色权限

  • 相关阅读:
    XAF 一对多关系<DC翻译博客二>
    XAF 在BOModel中实现接口<DC翻译博客六>
    XPO – (Gary's post)Stored Procedure Support Coming in V2010 Vol 2 Part1
    XAF 如何从Excel复制多个单元格内容到GridView
    XAF 如何实现对选择的单元格显示矩形框和多单元格的复制及粘贴
    XAF 如何扩展应用程序模型<二> 编辑ListView自动保存
    XAF 模型编辑器
    XAF 用代码扩展和自定义应用程序模型
    XAF 翻译领域构件(DC)技术目录
    XPO (Gary's post)Stored Procedure Support coming in V2010 Vol 2 (Part 2)
  • 原文地址:https://www.cnblogs.com/xyzla/p/2565390.html
Copyright © 2020-2023  润新知