3个回答
展开全部
可以把配置文件保存在*.ini配置文件中,下次读取即可
具体代码实现:
*.ini文件中配置如下:
例如:
[systemInfo]
Name=pc
pwd=123
首先创建一个读取ini通用类
/// <summary>
/// ini文件读写操作基础类
/// </summary>
public class IniFile
{
private static string IniFileName="./Config.ini";
[DllImport("kernel32")]
private static extern int GetPrivateProfileInt(string lpAppName, string lpKeyName,
int nDefault, string lpFileName);
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string lpAppName, string lpKeyName,
string lpDefault, StringBuilder lpReturnedString, int nSize, string lpFileName);
[DllImport("kernel32")]
private static extern bool WritePrivateProfileString(string lpAppName,
string lpKeyName, string lpString, string lpFileName);
//构造函数
public IniFile()
{
}
/// <summary>
/// 读取Ini配置文件
/// </summary>
/// <param name="Section"></param>
/// <param name="Key"></param>
/// <param name="Default"></param>
/// <returns></returns>
public int ReadIni(string Section, string Key, int Default)
{
return GetPrivateProfileInt(Section, Key, Default, IniFileName);
}
/// <summary>
/// 读取Ini配置文件
/// </summary>
/// <param name="Section"></param>
/// <param name="Key"></param>
/// <param name="Default"></param>
/// <returns></returns>
public string ReadIni(string Section, string Key, string Default)
{
StringBuilder temp = new StringBuilder(1024);
int rec = GetPrivateProfileString(Section, Key, Default, temp, 1024, IniFileName);
return temp.ToString();
}
public void WriteIni(string Section, string Key, int intValue)
{
WritePrivateProfileString(Section, Key, intValue.ToString(), IniFileName);
}
public void WriteIni(string Section, string Key, string strValue)
{
WritePrivateProfileString(Section, Key, strValue, IniFileName);
}
获取值为:
IniFile read =new IniFile();
string strName=read.ReadIni("systemInfo","Name","默认值")
string strPwd=read.ReadIni("systemInfo","pwd","默认值")
写值:
read.WriteIni("systemInfo","Name","你要输入的值");
具体代码实现:
*.ini文件中配置如下:
例如:
[systemInfo]
Name=pc
pwd=123
首先创建一个读取ini通用类
/// <summary>
/// ini文件读写操作基础类
/// </summary>
public class IniFile
{
private static string IniFileName="./Config.ini";
[DllImport("kernel32")]
private static extern int GetPrivateProfileInt(string lpAppName, string lpKeyName,
int nDefault, string lpFileName);
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string lpAppName, string lpKeyName,
string lpDefault, StringBuilder lpReturnedString, int nSize, string lpFileName);
[DllImport("kernel32")]
private static extern bool WritePrivateProfileString(string lpAppName,
string lpKeyName, string lpString, string lpFileName);
//构造函数
public IniFile()
{
}
/// <summary>
/// 读取Ini配置文件
/// </summary>
/// <param name="Section"></param>
/// <param name="Key"></param>
/// <param name="Default"></param>
/// <returns></returns>
public int ReadIni(string Section, string Key, int Default)
{
return GetPrivateProfileInt(Section, Key, Default, IniFileName);
}
/// <summary>
/// 读取Ini配置文件
/// </summary>
/// <param name="Section"></param>
/// <param name="Key"></param>
/// <param name="Default"></param>
/// <returns></returns>
public string ReadIni(string Section, string Key, string Default)
{
StringBuilder temp = new StringBuilder(1024);
int rec = GetPrivateProfileString(Section, Key, Default, temp, 1024, IniFileName);
return temp.ToString();
}
public void WriteIni(string Section, string Key, int intValue)
{
WritePrivateProfileString(Section, Key, intValue.ToString(), IniFileName);
}
public void WriteIni(string Section, string Key, string strValue)
{
WritePrivateProfileString(Section, Key, strValue, IniFileName);
}
获取值为:
IniFile read =new IniFile();
string strName=read.ReadIni("systemInfo","Name","默认值")
string strPwd=read.ReadIni("systemInfo","pwd","默认值")
写值:
read.WriteIni("systemInfo","Name","你要输入的值");
展开全部
1)在项目中添加一个设置文件:Settings1.settings
2)设置初始值
这里设置一个名为“Background"的配置项目,用来指定窗体的背景图片文件名
3)在程序中使用配置项,加载指定的图片
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
// 加载指定的图片作为窗体背景
this.BackgroundImage =
Image.FromFile(Properties.Settings.Default.Background);
}
……
4)窗体运行,用户修改了背景图片后,保存用户修改以便再次运行程序时,加载用户指定的图片。
using System;
using System.Drawing;
using System.Windows.Forms;
using System.IO;
namespace WindowsFormsApplication1
{
……
//用户点击button1,重新指定了背景图片
private void button1_Click(object sender, EventArgs e)
{
string dir = Path.GetDirectoryName(Properties.Settings.Default.Background);
string[] files = Directory.GetFiles(dir, "*.jpg");
int index = (new Random()).Next(0, files.Length);
this.BackgroundImage = Image.FromFile(files[index]);
Properties.Settings.Default.Background = files[index];
//保存用户指定图片文件的路径,以便下次运行时加载!
Properties.Settings.Default.Save();
}
}
更多追问追答
追问
background怎么定义?
追答
请参加见2)的图,图中标出了定义background的步骤
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你把路径保存在一个xml下面,下次启动再读取出来
追问
能给我参考代码么?大概思路我是知道的
追答
保存:
using (var write = new XmlTextWriter("xml路径", Encoding.UTF8))
{
var obj = "图片路径";
var serializer = new XmlSerializer(typeof(string));
serializer.Serialize(write, obj);
write.Close();
}
读取:
using (var reader = new XmlTextReader(xmlFile))
{
var serializer = new XmlSerializer(typeof(string));
var obj = (string)serializer.Deserialize(reader);
reader.Close();
return obj;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询