C# 操作 INI 自己工作笔记(对文本框的操作)
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 自己工作笔记(对文本框的操作)的更多相关文章
- Java + selenium 元素定位(6)之iframe切换(即对富文本框的操作)
在元素定位中,对富文本框的元素定位是特别的,当我们使用普通的元素定位方法对富文本框进行操作时,我们会发现不管我们之前介绍的八种方法中的任何方法,我们都不能成功定位到富文本框,并对其进行操作.那是因为富 ...
- Jquery学习笔记:操作form表单元素之一(文本框和下拉框)
一.概述 在web页面开发中,经常需要获取和设置表单元素的值(如文本框中的内容),特别是在ajax应用中,更是常态.本文系统的介绍下如何操作. 同操作其它html元素一样,操作的过程差不多. 第一步, ...
- 对pdf 表单域 或文本框的操作---动态填充PDF 文件内容
前提:需要pdf模板:并且模板内容以pdf 文本框的形式填写 package com.test;import java.io.File;import java.io.FileOutputStream; ...
- java学习:AWT组件和事件处理的笔记(1)--文本框上的ActionEvent事件
学习处理事件时,必须很好的掌握事件源,监视器,处理事件的接口 1.事件源 能够产生java认可事件的对象都可称为事件源,也就是说事件源必须是对象 2.监视器 监 ...
- java学习:AWT组件和事件处理的笔记(1)--文本框
java.awt包中的TextField类是专门用来建立文本框的,即TextField类创建的一个对象便是一个文本框. 1.TextField类的主要方法 (1)TextField() ...
- Android学习笔记(17):文本框TextView类
TextView继承自View.用于显示文本.它有很多的子类,掌握其属性是非常重要的. watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5 ...
- Android学习笔记-TextView(文本框)(二)
2.4 使用autoLink属性识别链接类型 当文字中出现了URL,E-Mail,电话号码,地图的时候,我们可以通过设置autoLink属性:当我们点击 文字中对应部分的文字,即可跳转至某默认APP, ...
- Android学习笔记-TextView(文本框)(一)
1.基础属性详解: id:为TextView设置一个组件id,根据id,我们可以在Java代码中通过findViewById()的方法获取到该对象,然后进行相关属性的设置,又或者使用RelativeL ...
- 【PyQt5-Qt Designer】文本框读写操作
主要内容: 1.读.写 输入控件(Input Widgets)中的内容(str) 2.保存数据到txt文件 3.从txt文件中读内容,与输入控件中内容比较 将上述各种输入控件(Input Widget ...
随机推荐
- [Android]异常7-Error:Configuration with name 'default' not found.
背景:使用SVN更新代码,运行出现 异常原因: 可能一>缺少Modules 解决办法有: 解决一>Android Studio切换为Project,settings.gradle中引用和现 ...
- MySQL 执行计划中Extra的浅薄理解
1.using where: Extra中出现“Using where”,通常来说,意味着全表扫描或者在查找使用索引的情况下,但是还有查询条件不在索引字段当中. 如果需要回表也是用这个. 2.usin ...
- JS——offset
1.offsetWidth.offsetHeight返回盒子宽度和高度,包括padding与border,不包括margin 2.offsetLeft.offsetTop返回盒子距离定位盒子的x轴方向 ...
- CSS——img
img标签初始化:在低版本的ie浏览器会自带边框,所以建议border:0px.
- N的阶乘末尾有多少个零?
在创联ifLab的招新问答卷上看到这么一题 核心问题是: 求N!(N的阶乘)的末尾有多少个零? 由于在N特别大的时候强行算出N!是不可能的,所以肯定要另找方法解决了. 首先,为什么末尾会有0?因为2* ...
- 解决hash冲突的三个方法-考虑获取
哈希表值的获取要考虑全部可能空间. 在链地址法中,可能空间就是具有相同hash值的链表. 目录 开放定址法 线性探测再散列 二次探测再散列 伪随机探测再散列 再哈希法 链地址法 建立公共溢出区 优 ...
- 洛谷——P2921 [USACO08DEC]在农场万圣节Trick or Treat on the Farm
P2921 [USACO08DEC]在农场万圣节Trick or Treat on the Farm 题意翻译 题目描述 每年,在威斯康星州,奶牛们都会穿上衣服,收集农夫约翰在N(1<=N< ...
- HTML学习笔记之标签进阶
目录 1.框架 2.表单 3.音频 4.视频 5.文档类型 6.头部元素 7.样式 8.脚本 9.实体 1.框架 使用框架允许我们在同一个浏览器窗口中显示多个页面,其中每份 HTML 文档称为一个框架 ...
- System----堡垒机
你知道嘛是堡垒机吗? 你知道堡垒机是奏嘛的吗? 1,改server 端 socket server 接受到的请求 执行指令前,记录收到的指令,来源ip 用户名 缺点:每台机器都要更改源码,加入指令记录 ...
- 【郑轻邀请赛 C】DOBRI
[题目链接]:https://acm.zzuli.edu.cn/zzuliacm/problem.php?id=2129 [题意] [题解] NMB 直接说i-1,i-2,i-3不就好了- [Numb ...