1.设置Config文件。新建Txt,改名为下图:
配置文件.config
2.配置Config文件。编写配置内容:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<ftp>
<FtpIp desc="IP地址">172.26.135.52</FtpIp>
<FtpHost desc="Ftp端口">21</FtpHost>
<FtpUser desc="FTP用户名">longwenh</FtpUser>
<FtpPass desc="FTP密码">123456</FtpPass>
</ftp>
</configuration>
3.使用Config文件。新建一个函数:
/// <summary>
/// 在config读取FTP配置
/// </summary>
/// <returns>返回FTP配置信息,结果状态成功返回1,失败返回0</returns>
public System.Collections.Generic.Dictionary<System.String, System.String> GetFtpMessage() {
System.Collections.Generic.Dictionary<System.String, System.String> dic_result = new System.Collections.Generic.Dictionary<System.String, System.String>();
dic_result.Add("state", "1");//结果状态:1成功,0失败
dic_result.Add("msg", "成功");//结果详情。
try
{
System.String str_xml_path = this.Get_WindowService_Path() + "\\config" + "\\配置文件.config";
System.Xml.XmlDocument xmlDocument = new System.Xml.XmlDocument();
xmlDocument.Load(str_xml_path);
System.Xml.XmlNode xmlNode = xmlDocument.SelectSingleNode("configuration/ftp");//取Ftp目录
str_ftp_server = xmlNode.SelectSingleNode("FtpIp").InnerText;//取FTP服务器地址
str_ftp_port = xmlNode.SelectSingleNode("FtpHost").InnerText;//取FTP服务器地址
str_ftp_server = System.String.Format("FTP://{0}:{1}/", str_ftp_server, str_ftp_port);
str_ftp_user = xmlNode.SelectSingleNode("FtpUser").InnerText;//FTP登录的用户名称
str_ftp_pass = xmlNode.SelectSingleNode("FtpPass").InnerText;//FTP登录的密码
dic_result.Add("str_ftp_server", str_ftp_server);
dic_result.Add("str_ftp_port", str_ftp_port);
dic_result.Add("str_ftp_user", str_ftp_user);
dic_result.Add("str_ftp_pass", str_ftp_pass);
dic_result.Add("str_ftp_upload_path", str_ftp_upload_path);
}
catch (Exception ex)
{
dic_result["state"] = "0";
dic_result["msg"] = ex.Message;
}
return dic_result;
}
实际项目使用案例:
感谢观看;
作者:龙文浩 C#工程师+Java工程师