Rename solution directory to Elwig
This commit is contained in:
		
							
								
								
									
										45
									
								
								Elwig/Helpers/TempFile.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										45
									
								
								Elwig/Helpers/TempFile.cs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,45 @@
 | 
			
		||||
using System;
 | 
			
		||||
using System.IO;
 | 
			
		||||
 | 
			
		||||
namespace Elwig.Helpers {
 | 
			
		||||
    public sealed class TempFile : IDisposable {
 | 
			
		||||
        private int Usages = 0;
 | 
			
		||||
        public string FilePath { get; private set; }
 | 
			
		||||
 | 
			
		||||
        public TempFile() : this(null) { }
 | 
			
		||||
 | 
			
		||||
        public TempFile(string? ext) : this(Path.Combine(Path.GetTempPath(), "Elwig"), ext) { }
 | 
			
		||||
 | 
			
		||||
        public TempFile(string dir, string? ext) {
 | 
			
		||||
            FilePath = Path.Combine(dir, Path.GetRandomFileName().Replace(".", "") + (ext != null ? $".{ext}" : ""));
 | 
			
		||||
            Usages++;
 | 
			
		||||
            Create();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        ~TempFile() {
 | 
			
		||||
            Delete();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public void Dispose() {
 | 
			
		||||
            if (--Usages == 0) {
 | 
			
		||||
                Delete();
 | 
			
		||||
                GC.SuppressFinalize(this);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public TempFile NewReference() {
 | 
			
		||||
            Usages++;
 | 
			
		||||
            return this;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void Create() {
 | 
			
		||||
            using (File.Create(FilePath)) { };
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void Delete() {
 | 
			
		||||
            if (FilePath == null) return;
 | 
			
		||||
            File.Delete(FilePath);
 | 
			
		||||
            FilePath = null;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user