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. ibatis知识点

    1:ibatis是apache的一个开源的项目,是一个O/R mapping解决方案,优点,小巧,灵活.2:搭建环境:导入ibatis相关jar包,jdbc驱动包等3:配置文件: jdbc连接的属性文 ...

  2. CSS——◇demo

    核心思想:嵌套盒子中的◇超过父盒子的部分隐藏. 第一种写法: <!DOCTYPE html> <html> <head> <meta charset=&quo ...

  3. SSH整合框架+mysql简单的实现

    SSH整合框架+mysql简单的实现 1. 框架整合原理: struts2整合Spring 两种: 一种struts2自己创建Action,自动装配Service : 一种 将Action交给Spri ...

  4. Windows 8 常见教程

    http://www.codeproject.com/Articles/439874/Web-service-on-Windows-Phone http://www.c-sharpcorner.com ...

  5. Day 13 进程和线程

    进程和线程 今天我们使用的计算机早已进入多CPU或多核时代,而我们使用的操作系统都是支持“多任务”的操作系统,这使得我们可以同时运行多个程序,也可以将一个程序分解为若干个相对独立的子任务,让多个子任务 ...

  6. 【特 性】Attribute

    1 AttributeUsage [AttributeUsageAttribute(AttributeTargets.All, AllowMultiple = true, Inherited = tr ...

  7. DP背包问题小总结

    DP的背包问题可谓是最基础的DP了,分为01背包,完全背包,多重背包 01背包 装与不装是一个问题 01背包基本模型,背包的总体积为v,总共有n件物体,每件物品的体积为v[i],价值为w[i],每件物 ...

  8. Linux自动化之Cobbler补鞋匠安装

    cobbler介绍:   快速网络安装linux操作系统的服务,支持众多的Linux发行版:Red Hat.   Fedora.CentOS.Debian.Ubuntu和SuSE,也可以支持网络安装w ...

  9. linux 头文件和库文件的设置

    GCC/G++会查找系统默认的include和link的路径,以及自己在编译命令中指定的路径.自己指定的路径就不说了,这里说明一下系统自动搜索的路径. [1]include头文件路径 除了默认的/us ...

  10. BUPT2017 springtraining(16) #4 ——基础数论

    题目在这里 A.手动打表找规律得组合数 n -= 2, m -= 2, ans = C(n, m) #include <bits/stdc++.h> using namespace std ...