Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Class Loggs
- {
- private string path;
- private string CreateDefaultPath()
- {
- string directory = Directory.GetCurrentDirectory() + @"/Logs/" + DateTime.Now.ToShortDateString();
- string fileName = "log.txt";
- string path;
- if (!Directory.Exists(directory))
- try
- {
- Directory.CreateDirectory(directory);
- }
- catch (IOException e)
- {
- Console.WriteLine("Can't create folder." + e.Message);
- }
- catch (UnauthorizedAccessException e)
- {
- Console.WriteLine("Can't create folder. Access denied." + e.Message);
- }
- path = Path.Combine(directory, fileName);
- return path;
- }
- private string CreateCustomPath(string directory, string fileName)
- {
- string path;
- string fullDirectory = directory + @"/" + DateTime.Now.ToShortDateString();
- if (IsValidFilename(fileName) && IsValidDirectory(fullDirectory))
- {
- if (!Directory.Exists(fullDirectory))
- try
- {
- Directory.CreateDirectory(fullDirectory);
- }
- catch (IOException)
- {
- Console.WriteLine("Something went wrong." + e.Message);
- }
- catch (UnauthorizedAccessException)
- {
- Console.WriteLine("Can't create file or folder. Access denied." + e.Message);
- }
- }
- else
- {
- throw new ArgumentException("Wrong name of folder or file");
- }
- path = Path.Combine(directory, fileName);
- return path;
- }
- private bool IsValidFilename(string fileName)
- {
- if (fileName.IndexOfAny(Path.GetInvalidFileNameChars()) >= 0 || String.IsNullOrEmpty(fileName))
- return false;
- return true;
- }
- private bool IsValidDirectory(string directory)
- {
- if (directory.IndexOfAny(Path.GetInvalidPathChars()) >= 0 || String.IsNullOrEmpty(directory))
- return false;
- return true;
- }
- #region constructors
- public Loggs()
- {
- path = CreateDefaultPath();
- }
- public Loggs(string directory, string fileName)
- {
- path = CreateCustomPath(directory, fileName);
- }
- #endregion
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement