从 .INI 文件加载默认值

发布于 2024-12-09 20:50:22 字数 2066 浏览 1 评论 0原文

一些背景知识:

我目前正在开发一个应用程序,该应用程序允许计算机新手用户无需进入命令提示符即可测试其 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 文件将存储在哪里?

也欢迎对现有代码提出任何意见。

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

非常感谢, J

A bit of background:

I am currently working on an application that allows novice computer users to test their ping without having to go into the command prompt.

My application works, but I would very much like to take the application to the next level and feed in default form values from a locally stored .INI file.

I can give people the existing code, but I stress that this application works - I am just interested in advancing the code so I can read in default form values.

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)
    {
    }
  }
}

I am uncertain how to go about it? Where would the default.ini file be stored for my application?

Also any comments on the existing code are welcomed.

If anyone can help I would be grateful.

Many Thanks,
J

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

千柳 2024-12-16 20:50:22

您可以将默认值存储在 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;
}

make像这样的 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

请告诉我如果这不清楚

我 理解希望它能帮助你......

You can store your default values in ini file (i.e config file), this default file will be stored in your system D or C folder...

and from that file you can get those default values from the ini file by the following method

 /// <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;
}

make a class like this INifile.cs and place the below code in 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();
    }
 }

and the values in config file look like this ....

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

you can get this values like by using

string a = getIniValue("System", "Sitecode", "");

you will get the value like this 1234

pls let me know if this is unclear to understand

i hope it will helps you......

幸福还没到 2024-12-16 20:50:22

如果您使用的是 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 数据绑定》一书。

If you are using Visual Studio 2005/2008 or 2010, using INI might not be a good choice. Instead you can use the off-the-shelf tools provided by Visual Studio, by clicking:

Project> Properties > Settings Tab

enter image description here

where you can add user settings and bind it to your GUI form. Visual Studio takes care of most of the stuff for you, and when you want to reference that variable, use the below syntax:

string LocalString=Properties.Settings.Default.YourSettings;

Moreover, a single call helps to save all the staff to archives.

Properties.Settings.Default.Save();

For even more details, please refer to the book Windows Form 2.0 Data Binding.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文