我用c#(winform)做一个发邮件的东东,想把一些配置信息如:SMTP地址,用户名,密码等信息写到资源文件里这样可以吗??如过不可以,最好是怎么处理???谢谢回答

解决方案 »

  1.   

    写道注册表里比较好
    当然写道config文件里面也行,不过最好另写一个xml文件
      

  2.   

    可以写到app.config中
    <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");         } 
      

  3.   

    再给出例子
    读:
    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);
      

  4.   

    写资源肯定是可以的,
    http://blog.csdn.net/lizanhong/archive/2004/09/10/100812.aspx
    不过写xml觉得更好,你也可以把xml文件存到资源文件。http://www.systemwebmail.com/default.aspx