Lucene.Net.Util.LuceneTestCase.CreateTempFile C# (CSharp) Method

CreateTempFile() public static method

Creates an empty file with the given prefix and suffix under the test class's #getBaseTempDirForTestClass().

The file will be automatically removed after the test class completes successfully. The test should close any file handles that would prevent the folder from being removed.

public static CreateTempFile ( string prefix, string suffix ) : FileInfo
prefix string
suffix string
return System.IO.FileInfo
        public static FileInfo CreateTempFile(string prefix, string suffix)
        {
            //DirectoryInfo @base = BaseTempDirForTestClass();

            int attempt = 0;
            FileInfo f;
            do
            {
                if (attempt++ >= TEMP_NAME_RETRY_THRESHOLD)
                {
                    throw new Exception("Failed to get a temporary name too many times, check your temp directory and consider manually cleaning it: " + System.IO.Path.GetTempPath());
                }
                f = new FileInfo(Path.Combine(System.IO.Path.GetTempPath(), prefix + "-" + string.Format(CultureInfo.InvariantCulture, "%03d", attempt) + suffix));
            } while (f.Create() == null);

            RegisterToRemoveAfterSuite(f);
            return f;
        }

Same methods

LuceneTestCase::CreateTempFile ( ) : FileInfo

Usage Example

Example #1
0
        internal static string MaybeCreateTempFile(bool removeAfterClass = true)
        {
            string result = null;
            Stream temp   = null;

            if (LuceneTestCase.TestLineDocsFile == LuceneTestCase.DEFAULT_LINE_DOCS_FILE) // Always GZipped
            {
                temp = typeof(LineFileDocs).FindAndGetManifestResourceStream(LuceneTestCase.TestLineDocsFile);
            }
            else if (LuceneTestCase.TestLineDocsFile.EndsWith(".gz", StringComparison.Ordinal))
            {
                temp = new FileStream(LuceneTestCase.TestLineDocsFile, FileMode.Open, FileAccess.Read, FileShare.Read);
            }
            if (null != temp)
            {
                var file = removeAfterClass
                    ? LuceneTestCase.CreateTempFile(TEMP_FILE_PREFIX, TEMP_FILE_SUFFIX)
                    : FileSupport.CreateTempFile(TEMP_FILE_PREFIX, TEMP_FILE_SUFFIX);
                result = file.FullName;
                using (var gzs = new GZipStream(temp, CompressionMode.Decompress, leaveOpen: false))
                    using (Stream output = new FileStream(result, FileMode.Open, FileAccess.Write, FileShare.Read))
                    {
                        gzs.CopyTo(output);
                    }
            }
            return(result);
        }
All Usage Examples Of Lucene.Net.Util.LuceneTestCase::CreateTempFile