我用c#(winform)做一个发邮件的东东,想把一些配置信息如:SMTP地址,用户名,密码等信息写到资源文件里这样可以吗??如过不可以,最好是怎么处理???谢谢回答
解决方案 »
- c#中的show()方法和showLog方法的返回值都是什么呢
- WINDOWS窗体应用程序与WPF应用程序的区别?
- asp调用存储过程遇到的问题,小弟先感谢前辈了!(在线等待)
- c#问题帮忙解决下不胜感激啊!!!!!
- datatable.Merge()的作用
- 怎样在邮件正文里添加图片?
- fckeditor控件上传图片功能,怎么能自动生成小图?
- 恳请问各位C#高手!!!!急
- 一个解决方案中的所有项目中的类都可以互相直接引用,不用using!对吗?
- 新建的C#windows应用程序编译后,如何能在其他的机器上运行?老是说没有动态库的连接。
- 还是C#中用弹出窗口来对DataGrid的某一列进行修改的问题
- 寻求blog,要求不是很高,只要支持多用户,能发文章,发评论/aspx格式...
当然写道config文件里面也行,不过最好另写一个xml文件
<appSettings>
<add key="SMTP" value="smtp.263.com"/>
<add key="MailName" value="Xman"/>
<add key="MailPwd" value="XmanPwd"/>
</appSettings> 然后用下面两个方法来读写:
/// <summary>
/// 读取.exe.config的值
/// </summary>
/// <param name="path">.exe.config文件的路径</param>
/// <param name="appKey">"key"的值</param>
/// <returns>"value"的值</returns>
internal static string GetConfigValue(string path,string appKey)
{
XmlDocument xDoc = new XmlDocument();
xDoc.Load(path); XmlNode xNode;
XmlElement xElem; xNode = xDoc.SelectSingleNode("//appSettings"); xElem = (XmlElement)xNode.SelectSingleNode("//add[@key=\"" + appKey + "\"]");
if ( xElem != null )
return xElem.GetAttribute("value");
else
return "";
}
/// <summary>
/// 设置.exe.config的值
/// </summary>
/// <param name="path">.exe.config文件的路径</param>
/// <param name="appKey">"key"的值</param>
/// <param name="appValue">"value"的值</param>
internal static void SetConfigValue(string path, string appKey, string appValue)
{
XmlDocument xDoc = new XmlDocument();
xDoc.Load(path); XmlNode xNode;
XmlElement xElem1;
XmlElement xElem2; xNode = xDoc.SelectSingleNode("//appSettings"); xElem1 = (XmlElement)xNode.SelectSingleNode("//add[@key=\"" + appKey + "\"]");
if ( xElem1 != null ) xElem1.SetAttribute("value",appValue);
else
{
xElem2 = xDoc.CreateElement("add");
xElem2.SetAttribute("key",appKey);
xElem2.SetAttribute("value",appValue);
xNode.AppendChild(xElem2);
}
xDoc.Save(System.Windows.Forms.Application.ExecutablePath + ".config"); }
读:
string smpt = GetConfigValue(Application.ExecutablePath + ".config","SMTP")
string mailName = GetConfigValue(Application.ExecutablePath + ".config","MailName")
string mailPwd = GetConfigValue(Application.ExecutablePath + ".config","MailPwd")写:
SetConfigValue(Application.ExecutablePath + ".config","SMTP",smpt);
SetConfigValue(Application.ExecutablePath + ".config","MailName",mailName);
SetConfigValue(Application.ExecutablePath + ".config","MailPwd",mailPwd);
http://blog.csdn.net/lizanhong/archive/2004/09/10/100812.aspx
不过写xml觉得更好,你也可以把xml文件存到资源文件。http://www.systemwebmail.com/default.aspx