Beispiel #1
0
        public static FileRef CreateManagedTemporaryFile(int fileSize, [PathReference]  string folderPath)
        {
            FileStream streamWriter = null;

            try
            {
                var filePath = GetPath(folderPath);
                var fileRef  = FileRef.Create(filePath);

                if (folderPath != string.Empty)
                {
                    // GetRandomFilePath unlike GetTempFileName does not create a file. We need to create and use its stream.
                    streamWriter = File.Create(filePath);
                }
                else
                {
                    // open stream to existing file
                    streamWriter = fileRef.OpenStreamWriter();
                }

                WriteRandomData(fileSize, streamWriter);
                return(fileRef);
            }
            finally
            {
                // this stream is no longer needed - we keep only file path later
                if (streamWriter != null)
                {
                    streamWriter.Close();
                    streamWriter.Dispose();
                }
            }
        }
Beispiel #2
0
        /// <summary>
        /// Changes files size. The content of the new file is random bytes.
        /// </summary>
        /// <param name="fileSize"></param>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static FileRef OverwriteFile(int fileSize, [PathReference] string filePath)
        {
            FileStream streamWriter = null;

            try
            {
                var fileRef = FileRef.Create(filePath);
                streamWriter = fileRef.OpenStreamWriter();

                WriteRandomData(fileSize, streamWriter);

                return(fileRef);
            }
            finally
            {
                // this stream is no longer needed - we keep only file path later
                if (streamWriter != null)
                {
                    streamWriter.Close();
                    streamWriter.Dispose();
                }
            }
        }
Beispiel #3
0
        public static FileRef CreateFile([PathReference] string filePath, int fileSize)
        {
            FileStream streamWriter = null;

            try
            {
                var path = Path.GetDirectoryName(filePath);
                Directory.CreateDirectory(path);

                streamWriter = File.Create(filePath);

                WriteRandomData(fileSize, streamWriter);
                return(FileRef.Create(filePath));
            }
            finally
            {
                // this stream is no longer needed - we keep only file path later
                if (streamWriter != null)
                {
                    streamWriter.Close();
                    streamWriter.Dispose();
                }
            }
        }
Beispiel #4
0
        public static TemporaryFile FromManagedFile(FileRef managedFile)
        {
            // TODO: perform validation? Or add similar construction methods?

            return(new TemporaryFile(managedFile.FullPath));
        }