【问题标题】:Loading default values from .INI Files从 .INI 文件加载默认值
【发布时间】:2011-12-08 07:36:15
【问题描述】:

一点背景:

我目前正在开发一个应用程序,该应用程序允许新手计算机用户无需进入命令提示符即可测试他们的 ping。

我的应用程序可以工作,但我非常希望将应用程序提升到一个新的水平,并从本地存储的 .INI 文件中输入默认表单值。

我可以向人们提供现有代码,但我强调此应用程序有效 - 我只是对改进代码感兴趣,以便可以读取默认表单值。

using System;
using System.Collections.Generic;
using System.Net;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.NetworkInformation;

namespace Ping_Application
{
  public partial class Form1 : Form
  {
    public Form1()
    {
        InitializeComponent();
    }

    private void pingButton_Click(object sender, EventArgs e)
    {
        if (pingAddressTextBox.Text != "")
        {
            DataTable resultsList = new DataTable();
            resultsList.Columns.Add("Time", typeof(int));
            resultsList.Columns.Add("Status", typeof(string));

            for (int indexVariable = 1; indexVariable <= timesToPing.Value; indexVariable++)
            {
                string stat = "";
                Ping pinger = new Ping();

                PingReply reply = pinger.Send(pingAddressTextBox.Text);
                if (reply.Status.ToString() != "Success")
                    stat = "Failed";
                else
                    stat = reply.RoundtripTime.ToString();
                pinger.Dispose();
                resultsList.Rows.Add(Convert.ToInt32(reply.RoundtripTime), reply.Status.ToString());
            }

            resultsGrid.DataSource = resultsList;

            minPing.Text = resultsList.Compute("MIN(time)", "").ToString();

            maxPing.Text = resultsList.Compute("MAX(time)", "").ToString();

            avgPing.Text = resultsList.Compute("AVG(time)", "").ToString();
        }
        else
        {
            MessageBox.Show("You are required to enter an address.");
        }
    }
    private void Form1_Load(object sender, EventArgs e)
    {
    }
  }
}

我不确定该怎么做?我的应用程序的 default.ini 文件将存储在哪里?

也欢迎现有代码上的任何 cmets。

如果有人可以提供帮助,我将不胜感激。

非常感谢, J

【问题讨论】:

    标签: c# visual-studio-2010 ini


    【解决方案1】:

    您可以将您的默认值存储在 ini 文件(即配置文件)中,该默认文件将存储在您的系统 D 或 C 文件夹中...

    从该文件中,您可以通过以下方法从 ini 文件中获取这些默认值

     /// <summary>
    /// This will read config.ini file and return the specific value
    /// </summary>
    /// <param name="MainSection">Main catergory name</param>
    /// <param name="key">name of the key in main catergory</param>
    /// <param name="defaultValue">if key is not in the section, then default value</param>
    /// <returns></returns>
    public static string getIniValue(string MainSection, string key, string defaultValue)
    {
      IniFile inif = new IniFile(AppDataPath() + @"\config.ini");
      string value = "";
    
      value = (inif.IniReadValue(MainSection, key, defaultValue));
      return value;
    }
    
    public static string AppDataPath()
    {
      gCommonAppDataPath = @"c:\" + gCompanyName + @"\" + gProductName; // your config file location path
      return gCommonAppDataPath;
    }
    

    创建一个像这样的 INifile.cs 类并将下面的代码放在 ini.cs 中

     public class IniFile
     {
        public string path;
    
        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section,string key,string val,string filePath);
        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section,string key,string def,StringBuilder retVal,int size,string filePath);
    
        /// <summary>
        /// INIFile Constructor.
        /// </summary>
        /// <param name="INIPath"></param>
        public IniFile(string INIPath)
        {
            path = INIPath;
        }
        /// <summary>
        /// Write Data to the INI File
        /// </summary>
        /// <param name="Section"></param>
        /// Section name
        /// <param name="Key"></param>
        /// Key Name
        /// <param name="Value"></param>
        /// Value Name
        public void IniWriteValue(string Section,string Key,string Value)
        {
            WritePrivateProfileString(Section,Key,Value,this.path);
        }
    
        /// <summary>
        /// Read Data Value From the Ini File
        /// </summary>
        /// <param name="Section"></param>
        /// <param name="Key"></param>
        /// <param name="Path"></param>
        /// <returns></returns>
        public string IniReadValue(string Section,string Key,string Default)
        {
            StringBuilder temp = new StringBuilder(255);
            int i = GetPrivateProfileString(Section,Key,Default,temp,255,this.path);
            return temp.ToString();
    
        }
        public void IniWriteString(string Section, string Key, string Value)
        {
            WritePrivateProfileString(Section, Key, Value, this.path);
        }
        public string IniReadString(string Section, string Key, string Default)
        {
            StringBuilder temp = new StringBuilder(255);
            int i = GetPrivateProfileString(Section, Key, Default, temp, 255, this.path);
            return temp.ToString();
        }
     }
    

    配置文件中的值看起来像这样......

      [System]
      GroupCode=xx
      SiteCode=1234
      MemberPrefix=xxx
      AutoStart=no
      EnablePosButton=yes....
    

    您可以使用

    来获取此值
    string a = getIniValue("System", "Sitecode", "");
    

    你会得到这样的值 1234

    如果不清楚,请告诉我

    希望对你有帮助......

    【讨论】:

    • 如何在我的 Form1.CS 顶部包含对 IniFile.cs 的使用?
    • 好吧,为您的项目中的ini.cs创建一个单独的文件夹,将其命名为classfloder,并为这两个方法“getIniValue”“AppDataPath”创建一个名为“helper”的单独类,然后您可以访问像这个字符串 a = classfolder.helper.getinivalue();......
    • 使用这些 api 是个坏主意,它们背后有大量的 Windows 3 appcompat。读取 .ini 文件的成本非常,每个设置大约需要 50 毫秒。
    • 业余问题。是否可以使用键名创建一个变量,然后为其分配“值”?
    【解决方案2】:

    如果您使用的是 Visual Studio 2005/2008 或 2010,则使用 INI 可能不是一个好的选择。相反,您可以使用 Visual Studio 提供的现成工具,方法是单击:

    项目> 属性 > 设置选项卡

    您可以在其中添加用户设置并将其绑定到您的 GUI 表单。 Visual Studio 会为您处理大部分内容,当您想要引用该变量时,请使用以下语法:

    string LocalString=Properties.Settings.Default.YourSettings;
    

    此外,一个电话有助于将所有员工保存到档案中。

    Properties.Settings.Default.Save();
    

    更多细节,请参考Windows Form 2.0 Data Binding一书。

    【讨论】:

      猜你喜欢
      • 2021-04-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-02
      • 1970-01-01
      • 2021-03-18
      相关资源
      最近更新 更多