Advertisement
Guest User

Untitled

a guest
Jan 11th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.68 KB | None | 0 0
  1. Class Loggs
  2. {
  3.    private string path;
  4.         private string CreateDefaultPath()
  5.         {
  6.             string directory = Directory.GetCurrentDirectory() + @"/Logs/" + DateTime.Now.ToShortDateString();
  7.             string fileName = "log.txt";
  8.             string path;
  9.             if (!Directory.Exists(directory))
  10.                 try
  11.                 {
  12.                     Directory.CreateDirectory(directory);
  13.                 }
  14.                 catch (IOException e)
  15.                 {
  16.                     Console.WriteLine("Can't create folder." + e.Message);
  17.                 }
  18.                 catch (UnauthorizedAccessException e)
  19.                 {
  20.                     Console.WriteLine("Can't create folder. Access denied." + e.Message);
  21.                 }
  22.                
  23.             path = Path.Combine(directory, fileName);
  24.             return path;
  25.         }
  26.         private string CreateCustomPath(string directory, string fileName)
  27.         {
  28.             string path;
  29.             string fullDirectory = directory + @"/" + DateTime.Now.ToShortDateString();
  30.  
  31.             if (IsValidFilename(fileName) && IsValidDirectory(fullDirectory))
  32.             {
  33.                 if (!Directory.Exists(fullDirectory))
  34.                     try
  35.                     {
  36.                         Directory.CreateDirectory(fullDirectory);
  37.                     }
  38.                     catch (IOException)
  39.                     {
  40.                         Console.WriteLine("Something went wrong." + e.Message);
  41.                     }
  42.                     catch (UnauthorizedAccessException)
  43.                     {
  44.                         Console.WriteLine("Can't create file or folder. Access denied." + e.Message);
  45.                     }
  46.             }
  47.             else
  48.             {
  49.                 throw new ArgumentException("Wrong name of folder or file");
  50.             }
  51.            
  52.             path = Path.Combine(directory, fileName);
  53.             return path;
  54.         }
  55.         private bool IsValidFilename(string fileName)
  56.         {
  57.             if (fileName.IndexOfAny(Path.GetInvalidFileNameChars()) >= 0 || String.IsNullOrEmpty(fileName))
  58.                 return false;
  59.             return true;
  60.         }
  61.         private bool IsValidDirectory(string directory)
  62.         {
  63.             if (directory.IndexOfAny(Path.GetInvalidPathChars()) >= 0 || String.IsNullOrEmpty(directory))
  64.                 return false;
  65.             return true;
  66.         }
  67. #region constructors
  68.         public Loggs()
  69.         {
  70.             path = CreateDefaultPath();
  71.         }
  72.         public Loggs(string directory, string fileName)
  73.         {
  74.             path = CreateCustomPath(directory, fileName);
  75.         }
  76. #endregion
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement