IIS在创建文件后不释放文件

时间:2013-06-06 15:40:46

标签: asp.net iis io

我有一个在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 });
            }
        }

1 个答案:

答案 0 :(得分:0)

之前我遇到了File.*方法的问题,因为它会创建一个文件并为您返回StreamWriter,但是您必须等到GC处理它才能将它处理掉该文件已发布。

请尝试将File.CreateText()更改为使用FileStream并自行手动调用Close()

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

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

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