1

我有一个在 IIS 上运行的进程,它移动和归档文件,然后生成一些空的 *.cnf 文件,让另一个应用程序知道可以处理这些文件。不知何故,IIS 没有发布 *.cnf 文件。我已经粘贴了下面的代码。希望有人可以帮助我指出它是在我的代码中还是只是 IIS。

public void Run()
        {
            try
            {
                // Log start of job
                _log.Info("GL file copy job started ...");

                // Check if today's date is not a holiday before processing these GL files
                if (!_prov.IsGlHoliday())
                {
                    // Get tmp directory
                    string glTempDirPath = GLConstants.Directories.GLTmp;

                    // Copy files from tmp to production drop off directory
                    if (Directory.Exists(glTempDirPath))
                    {
                        // Get tmp directory information
                        DirectoryInfo glTempDirInfo = new DirectoryInfo(glTempDirPath);

                        // Get all files at directory root level only
                        FileInfo[] glTempFiles = glTempDirInfo.GetFiles("*", SearchOption.TopDirectoryOnly);

                        // Check if any error files were generated during validation process
                        bool errorsFound = false;
                        foreach (FileInfo f in glTempFiles)
                        {
                            if (!f.Attributes.HasFlag(FileAttributes.Hidden) && f.Name.ToLower().IndexOf("error") != -1)
                            {
                                errorsFound = true;
                                break;
                            }
                        }

                        if (!errorsFound)
                        {
                            // Get GL data and confirmation files
                            List<FileInfo> glFiles = new List<FileInfo>();
                            foreach (FileInfo f in glTempFiles)
                            {
                                if (!f.Attributes.HasFlag(FileAttributes.Hidden) && f.Name.ToLower().IndexOf("error") == -1)
                                {
                                    glFiles.Add(f);
                                }
                            }
                            _log.Info(string.Format("Copy and archiving {0} GL file{1} ...", glFiles.Count.ToString(), (glFiles.Count > 0) ? "s" : string.Empty));

                            // Archive Data files only
                            // 1. Create archive directory if it does not exist
                            // 2. Move files from temporary directory to archive directory
                            string glArchiveDirPath = Path.Combine(GLConstants.Directories.GLArchive, DateTime.Now.ToString("MMddyyyy"));

                            DirectoryInfo glArchiveDirInfo = new DirectoryInfo(glArchiveDirPath);
                            if (!(glArchiveDirInfo.Exists)) { glArchiveDirInfo.Create(); }

                            foreach (FileInfo f in glFiles)
                            {
                                string srcDestination = f.FullName;
                                string archiveDestination = Path.Combine(glArchiveDirPath, f.Name);
                                f.CopyTo(archiveDestination, true);
                                _log.Info(string.Format("GL file archived from {0} to {1} ...", srcDestination, f.FullName));
                            }

                            // (1) Move data files
                            foreach (FileInfo f in glFiles)
                            {
                                string moveToDestination = Path.Combine(GLConstants.Directories.GL, f.Name);
                                f.MoveTo(moveToDestination);
                                _log.Info(string.Format("GL file moved from {0} to {1} ...", f.FullName, moveToDestination));
                            }

                            // (2) Create confirmation files (.cnf) for these data files (TIF, BIOS, INPAXRF)
                            List<string> cnfFiles = new List<string>();
                            foreach (FileInfo f in glFiles)
                            {
                                if (
                                    (f.Name.IndexOf("TIF", StringComparison.OrdinalIgnoreCase) != -1)
                                    ||
                                    (f.Name.IndexOf("BIOS", StringComparison.OrdinalIgnoreCase) != -1)
                                    ||
                                    (f.Name.IndexOf("INPAXRF", StringComparison.OrdinalIgnoreCase) != -1)
                                    )
                                {
                                    cnfFiles.Add(f.Name);
                                }
                            }

                            foreach (string f in cnfFiles)
                            {
                                string cnfFile = Path.Combine(GLConstants.Directories.GL, string.Format("{0}{1}", f, ".cnf"));
                                File.CreateText(cnfFile);
                            }                                                                                 

                            NotifyGLJobStatus.Send(new GLJobStatus { subject = "GL file copy", msg = "GL file copy process completed." });
                        }
                        else
                        {
                            _log.Info("One or more error files detected, GL file copy process terminated ...");
                            NotifyGLJobStatus.Send(new GLJobStatus { subject = "GL file copy", msg = "One or more error files detected, GL file copy process terminated." });
                        }
                    }
                    else
                    {
                        _log.Info(string.Format("{0} path does not exist, GL file copy process terminated ...", glTempDirPath));
                        NotifyGLJobStatus.Send(new GLJobStatus { subject = "GL file copy", msg = string.Format("{0} path does not exist, GL file copy process terminated.", glTempDirPath) });
                    }
                }
                else
                {
                    _log.Info(string.Format("{0} is a holiday, GL file copy process terminated ...", DateTime.Now.ToString("d")));
                    NotifyGLJobStatus.Send(new GLJobStatus { subject = "GL file copy", msg = string.Format("{0} is a holiday, GL file copy process terminated.", DateTime.Now.ToString("d")) });
                }

                // Log end of job
                _log.Info("GL file copy job ended ...");
            }
            catch (Exception ex)
            {
                _log.Fatal(string.Format("{0} : {1} ({2})", ex.Source, ex.Message, ex.ToString()));
                NotifyGLJobStatus.Send(new GLJobStatus { subject = "GL file copy (ERROR)", msg = ex.Message });
            }
        }
4

1 回答 1

0

我之前遇到过这些File.*方法的问题,因为它会创建一个文件并StreamWriter为您返回一个文件,但您必须等到 GC 开始处理它以释放文件。

而是尝试更改File.CreateText()为使用 aFileStream并手动调用Close()自己:

private static void CreateFile(string fileName)
{
    FileStream file = new FileStream(fileName,FileMode.Create,FileAccess.Write,FileShare.None);
    file.Close();
}

这也可以考虑它(没有尝试过):

File.CreateText(...).Close();

于 2013-06-06T15:45:00.360 回答