Esempio n. 1
0
        /// <summary>
        ///   Creates a new TempDir object, relative to this TempDir, using the supplied dirNameTemplate.
        ///   NOTE: This does not actually create the directory on the file system.
        /// </summary>
        /// <param name="dirNameTemplate">
        ///   A template string that is used to determine the name of the new TempDir.
        ///   It can have 1 of 3 possible values:
        ///     (1.) Empty           - a fully random name will be generated that is unique within the TempDir.
        ///     (2.) "temp_dir_{0}"  - a semi random name will be generated, replacing the {0} placeholder with some random, unique value.
        ///     (3.) "my_temp_dir"   - an exact, non-random name that will always be the same.
        /// </param>
        public TempDir NewTempDir(string dirNameTemplate = "")
        {
            string name    = TempUtils.GenerateFsEntryName(DirPath, dirNameTemplate);
            var    tempDir = new TempDir(DirPath, name);

            return(tempDir);
        }
Esempio n. 2
0
        /// <summary>
        ///   Creates a new TempFile object, relative to this TempDir, using the supplied fileNameTemplate, and registers
        ///   the new TempFile with this TempDir, so that we can later delete the registered TempFiles using the
        ///   ClearTempFiles() method.
        ///
        ///   NOTE: This does NOT actually create the file on the file system.
        /// </summary>
        /// <param name="fileNameTemplate">
        ///   A template string that is used to determine the name of the new TempFile.
        ///   It can have 1 of 3 possible values:
        ///     (1.) Empty                - a fully random name will be generated that is unique within the TempDir.
        ///     (2.) "temp_file_{0}.tmp"  - a semi random name will be generated, replacing the {0} placeholder with some random, unique value.
        ///     (3.) "my_temp_file"       - an exact, non-random name that will always be the same.
        /// </param>
        public TempFile NewTempFile(string fileNameTemplate = "")
        {
            string fileName = TempUtils.GenerateFsEntryName(DirPath, fileNameTemplate);
            var    tempFile = new TempFile(this, fileName);

            // keep track of temp files so that we can selectively clean them up later
            _files.Add(tempFile);

            return(tempFile);
        }
Esempio n. 3
0
        public void Rename(string fileNameTemplate = "")
        {
            string   newFileName = TempUtils.GenerateFsEntryName(_baseDir.DirPath, fileNameTemplate);
            TempFile toFile      = TempFile.NewFrom(this, newFileName);

            if (File.Exists(FilePath))
            {
                if (File.Exists(toFile.FilePath))
                {
                    File.Delete(toFile.FilePath);
                }

                File.Move(FilePath, toFile.FilePath);
            }

            _fileName = newFileName;
        }