using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.InteropServices;

namespace GHGD.UI
{
public partial class FrmBackUp : Form
{
public FrmBackUp()
{
InitializeComponent();
}

#region "声明变量"

/// <summary>
/// 写入INI文件
/// </summary>
/// <param name="section">节点名称[如[TypeName]]</param>
/// <param name="key">键</param>
/// <param name="val">值</param>
/// <param name="filepath">文件路径</param>
/// <returns></returns>
[DllImport("kernel32")]
private static extern long WritePrivateProfileString(string section,string key,string val,string filepath);
/// <summary>
/// 读取INI文件
/// </summary>
/// <param name="section">节点名称</param>
/// <param name="key">键</param>
/// <param name="def">值</param>
/// <param name="retval">stringbulider对象</param>
/// <param name="size">字节大小</param>
/// <param name="filePath">文件路径</param>
/// <returns></returns>
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section,string key,string def,StringBuilder retval,int size,string filePath);
//private string strFilePath = Application.StartupPath + "\\BackUpIni.ini";//获取INI文件路径
private string strFilePath = Application.StartupPath + "\\" + "BackUpIni.ini";//获取INI文件路径

private string strSec =""; //INI文件名

#endregion

//写入按钮事件
//private void btnWrite_Click(object sender, EventArgs e)
//{
// try
// {

// //根据INI文件名设置要写入INI文件的节点名称
// //此处的节点名称完全可以根据实际需要进行配置
// strSec = Path.GetFileNameWithoutExtension(strFilePath);
// WritePrivateProfileString(strSec, "Name", txt_ServerName.Text.Trim(), strFilePath);
// WritePrivateProfileString(strSec, "Sex", txt_DatabaseName.Text.Trim(), strFilePath);
// WritePrivateProfileString(strSec, "Age", txt_UserName.Text.Trim(), strFilePath);
// WritePrivateProfileString(strSec, "Address", txt_Password.Text.Trim(), strFilePath);
// WritePrivateProfileString(strSec, "Address", txt_BackUpPath.Text.Trim(), strFilePath);
// MessageBox.Show("写入成功");
// }catch(Exception ex){
// MessageBox.Show(ex.Message.ToString());
// }
//}

//读取按钮事件
//private void btnRead_Click(object sender, EventArgs e)
//{
//if (File.Exists(strFilePath))//读取时先要判读INI文件是否存在
//{
// //[BackUpIni]
// strSec = Path.GetFileNameWithoutExtension(strFilePath);
// txt_ServerName.Text = ContentValue(strSec, "ServerName");
// txt_DatabaseName.Text = ContentValue(strSec, "DatabaseName");
// txt_UserName.Text = ContentValue(strSec, "UserName");
// txt_Password.Text = ContentValue(strSec, "Password");
// txt_BackUpPath.Text = ContentValue(strSec, "BackUpPath");
//}
//else {
// MessageBox.Show("INI文件不存在");
//}
//}

/// <summary>
/// 自定义读取INI文件中的内容方法
/// </summary>
/// <param name="Section">键</param>
/// <param name="key">值</param>
/// <returns></returns>
private string ContentValue(string Section,string key) {

StringBuilder temp = new StringBuilder(1024);
int i = GetPrivateProfileString(Section, key, "", temp, 1024, strFilePath);
return temp.ToString();
}

private void FrmBackUp_Load(object sender, EventArgs e)
{
//MessageBox.Show(strFilePath);
if (File.Exists(strFilePath))//读取时先要判读INI文件是否存在
{
//[BackUpIni]
strSec = Path.GetFileNameWithoutExtension(strFilePath);
txt_ServerName.Text = ContentValue(strSec, "ServerName");
txt_DatabaseName.Text = ContentValue(strSec, "DatabaseName");
txt_UserName.Text = ContentValue(strSec, "UserName");
txt_Password.Text = ContentValue(strSec, "Password");
txt_BackUpPath.Text = ContentValue(strSec, "BackUpPath");
}
else
{
//类的构造函数,传递INI文件名
//public IniFiles(string AFileName)
//{
       // 判断文件是否存在
FileInfo fileInfo = new FileInfo(strFilePath);
       if ((!fileInfo.Exists))
       { //|| (FileAttributes.Directory in fileInfo.Attributes))
        //文件不存在,建立文件
System.IO.StreamWriter sw = new System.IO.StreamWriter(strFilePath, false, System.Text.Encoding.Default);
        try
        {
           sw.Write("#备份数据文档");
           sw.Close();
        }
        catch
        {
           throw (new ApplicationException("Ini文件不存在"));
        }
       }
      //必须是完全路径,不能是相对路径
strFilePath = fileInfo.FullName;
MessageBox.Show("strFilePath: " + strFilePath);

strSec = Path.GetFileNameWithoutExtension(strFilePath);
MessageBox.Show("strSec: " + strSec);
txt_ServerName.Text = ContentValue(strSec, "ServerName");
txt_DatabaseName.Text = ContentValue(strSec, "DatabaseName");
txt_UserName.Text = ContentValue(strSec, "UserName");
txt_Password.Text = ContentValue(strSec, "Password");
txt_BackUpPath.Text = ContentValue(strSec, "BackUpPath");

//}
//MessageBox.Show("INI文件不存在");
}
}

private void btn_BackUp_ok_Click(object sender, EventArgs e)
{
try
{
//根据INI文件名设置要写入INI文件的节点名称
//此处的节点名称完全可以根据实际需要进行配置
WritePrivateProfileString("BackUpIni", "ServerName", txt_ServerName.Text.Trim(), strFilePath);
WritePrivateProfileString("BackUpIni", "DatabaseName", txt_DatabaseName.Text.Trim(), strFilePath);
WritePrivateProfileString("BackUpIni", "UserName", txt_UserName.Text.Trim(), strFilePath);
WritePrivateProfileString("BackUpIni", "Password", txt_Password.Text.Trim(), strFilePath);
WritePrivateProfileString("BackUpIni", "BackUpPath", txt_BackUpPath.Text.Trim(), strFilePath);
MessageBox.Show("写入成功");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
this.Close();
}

private void btn_BackUp_cancle_Click(object sender, EventArgs e)
{
this.Close();
}
}
}

C# 操作 INI 自己工作笔记(对文本框的操作)的更多相关文章

  1. Java + selenium 元素定位(6)之iframe切换(即对富文本框的操作)

    在元素定位中,对富文本框的元素定位是特别的,当我们使用普通的元素定位方法对富文本框进行操作时,我们会发现不管我们之前介绍的八种方法中的任何方法,我们都不能成功定位到富文本框,并对其进行操作.那是因为富 ...

  2. Jquery学习笔记:操作form表单元素之一(文本框和下拉框)

    一.概述 在web页面开发中,经常需要获取和设置表单元素的值(如文本框中的内容),特别是在ajax应用中,更是常态.本文系统的介绍下如何操作. 同操作其它html元素一样,操作的过程差不多. 第一步, ...

  3. 对pdf 表单域 或文本框的操作---动态填充PDF 文件内容

    前提:需要pdf模板:并且模板内容以pdf 文本框的形式填写 package com.test;import java.io.File;import java.io.FileOutputStream; ...

  4. java学习:AWT组件和事件处理的笔记(1)--文本框上的ActionEvent事件

    学习处理事件时,必须很好的掌握事件源,监视器,处理事件的接口    1.事件源        能够产生java认可事件的对象都可称为事件源,也就是说事件源必须是对象    2.监视器        监 ...

  5. java学习:AWT组件和事件处理的笔记(1)--文本框

    java.awt包中的TextField类是专门用来建立文本框的,即TextField类创建的一个对象便是一个文本框. 1.TextField类的主要方法    (1)TextField()      ...

  6. Android学习笔记(17):文本框TextView类

    TextView继承自View.用于显示文本.它有很多的子类,掌握其属性是非常重要的. watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5 ...

  7. Android学习笔记-TextView(文本框)(二)

    2.4 使用autoLink属性识别链接类型 当文字中出现了URL,E-Mail,电话号码,地图的时候,我们可以通过设置autoLink属性:当我们点击 文字中对应部分的文字,即可跳转至某默认APP, ...

  8. Android学习笔记-TextView(文本框)(一)

    1.基础属性详解: id:为TextView设置一个组件id,根据id,我们可以在Java代码中通过findViewById()的方法获取到该对象,然后进行相关属性的设置,又或者使用RelativeL ...

  9. 【PyQt5-Qt Designer】文本框读写操作

    主要内容: 1.读.写 输入控件(Input Widgets)中的内容(str) 2.保存数据到txt文件 3.从txt文件中读内容,与输入控件中内容比较 将上述各种输入控件(Input Widget ...

随机推荐

  1. O-理解共享池

    我们可以通过show sga命令查看共享池的整体组成部分: ....待截图.... 一.SGA内存结构 Oracle中SGA主要包括: 1.固定数据结构部分(FIXED Size) 2.数据块缓冲区( ...

  2. DOM对象之window

    window的属性 top:返回当前窗口的最顶层的先辈窗口 document:返回HTML文档对象 location:当前窗口的地址 self:返回对自身窗口的引用 parent:返回父窗口 如何引用 ...

  3. 扩增子图表解读6韦恩图:比较组间共有和特有OTU或分类单元

    韦恩图 Venn Diagram Venn Diagram,也称韦恩图.维恩图.文氏图,用于显示元素集合重叠区域的图示.   韦图绘制工具 常用R语言的VennDiagram包绘制,输出PDF格式方便 ...

  4. Win10电脑如何更改开机启动项

    https://jingyan.baidu.com/article/5970355284f0458fc1074049.html

  5. 学不好Linux?我们分析看看正确的学习方法是什么-马哥教育

    2018年里,Linux运维的职位数量和平均薪资水平仍然持续了去年的强劲增幅,比很多开发岗位涨的都快.从研究机构的数据来看,Linux职位数量和工资水平涨幅均在IT行业的前五之列,比去年的表现还要好一 ...

  6. pop的运用

    pop():弹出列表最后一个元素 练习题: num_list = [12, 45, 34,13, 100, 24, 56, 74, 109] one_list = [] two_list = [] t ...

  7. [系统资源攻略]CPU

    linux系统中如何查看cpu信息? 查看linux版本.cpu.位数.内核.内存等信息 linux下查看CPU,内存,机器型号,网卡等信息的方法 查看服务器物理CPU数和CPU核数方法介绍 可以用/ ...

  8. centos 7中 yum安装jdk

    yum安装jdk的好处就是不需要手动再配置环境变量,所有的变量都是自动生成 1.检查系统是否存在jdk,存在删除原版jdk 如果没有信息输出,则表示没有安装jdk rpm -qa |grep java ...

  9. Django基础配置

    安装djangopip install Django==1.11.4进入pythonimport django查看版本号django.get_version()创建项目,在合适位置创建一个目录进入你要 ...

  10. padding填充与box-sizing: border-box配合使用

    不管伸缩盒还是浮动盒子,只要使用到padding,就必须使用 box-sizing: border-box;     有图片的时候,需摇与其他文字对齐的时候,在图片的外层加个:vertical-ali ...