我正在用 C# 编写一个程序,它将连接到一个 FTP 位置并读取一个 XML 文件,并希望在本地对其进行测试。
我该如何在我的机器上创建一个 FTP 站点以便我可以做到这一点?
我的程序代码(可能不需要回答这个问题?)是:
private string LoadFTPFile()
{
string xml = null;
string ftpAddress = "ftp://" + ftpLocation + "/" + PrintMachineParameters_BaseName + ".xml";
try
{
logging.Write("Info", "LoadFTPFile()", "XML loaded from PLC: " + ftpAddress);
WebClient request = new WebClient();
request.Credentials = new NetworkCredential(ftpUserName, ftpPassword);
byte[] fileData = request.DownloadData(ftpAddress);
xml = System.Text.Encoding.Default.GetString(fileData);
try
{
XmlDocument xmlDoc = new XmlDocument();
xml = xml.Substring(xml.IndexOf("<"));
xmlDoc.LoadXml(xml);
XmlNode xmlNode = xmlDoc;
xml = xmlNode.InnerXml;
}
catch (Exception exception)
{
string message = "Error loading FTP file: " + ftpAddress + Environment.NewLine;
DisplayException("Error loading FTP file", message, exception);
xml = null;
}
}
catch (Exception exception)
{
string message = "Error loading FTP file: " + ftpAddress + Environment.NewLine;
DisplayException("Error loading FTP file", message, exception);
xml = null;
}
return xml;
}
谢谢。