我们遇到一个问题,我们有一个函数被调用来创建FTP设置文件,但有时这个函数永远不会被调用(因为FTP设置文件从不存在),这个问题是间歇性的。在
之后调用此函数 Directory.CreateDirectory(System.Web.HttpContext.Current.Server.MapPath(UploadPath));
public bool GenerateFTPSettings()
{
try
{
using (System.IO.StreamWriter file = new System.IO.StreamWriter(HttpContext.Current.Server.MapPath((string)HttpContext.Current.Session["UploadPath"]) + @"\ftp_settings.ftp"))
{
file.WriteLine(string.Format("user {0}",ConfigurationManager.AppSettings["FTPUserName"]));
file.WriteLine(ConfigurationManager.AppSettings["FTPPassword"]);
file.WriteLine(string.Format("QUOTE SITE LRECL=80 RECFM=FB CY PRI={0} SEC={1}", ConfigurationManager.AppSettings["PRI"], ConfigurationManager.AppSettings["SEC"]));
file.WriteLine("BIN");
file.WriteLine(string.Format("put {0}\\NEW FILE'FTPP.Z.T.{1}.{2}.INT'", HttpContext.Current.Server.MapPath((string)HttpContext.Current.Session["UploadPath"]),
ConfigurationManager.AppSettings["FTPUserName"], ConfigurationManager.AppSettings["MailboxID"]));
file.WriteLine("QUIT");
}
return true;
}
catch (Exception ex)
{
ErrorHandler.WriteError(ex, "Upload");
return false;
}
}
答案 0 :(得分:1)
我看到它可能是两件事。确保像这样刷新你的流:
using (System.IO.StreamWriter file = new System.IO.StreamWriter("somefile")
{
//write to the stream
file.Flush();
}
您还确定会话有值:
HttpContext.Current.Session["UploadPath"]
是否总是填充或者是否会从会话中丢失?我会拆分逻辑以确保它设置如下:
string uploadPath = HttpContext.Current.Session["UploadPath"];
if (!string.IsNullOrEmpty())
{
//write to the file
}