1

我们遇到了一个问题,我们有一个函数被调用来创建 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;
    }
}
4

1 回答 1

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
}
于 2012-09-10T16:27:24.043 回答