GreatScottIsNotMyName
GreatScottIsNotMyName

Reputation: 101

"Can't create Directory as the directory doesn't exist"

This is my logging.cs, It usually may create the "Log-Folder" and the Datetime.csv into the users Desktop

public static class Logging
{
    public static string _Path = $"C:\\Users\\{Environment.UserName}\\Desktop\\Logs\\{DateTime.Now.ToString("dd.MM.yyyy")}.csv";
    static StreamWriter _File = new StreamWriter(_Path);

    public static void getPath(string path)
    {
        if (!Directory.Exists(path))
        {
            Directory.CreateDirectory(path);
        }
    }

    public static void logging(string message)
    {
        _File.Write(message);
    }
}

In my main class, i use the method logging just to enter "Test" into the csv file

class Program
{
    static void Main(string[] args)
    {
        Logging.getPath(Logging._Path);
        Logging.logging("Test");
    }
}

but when there is no "Logs-Folder", i get the exception that part of the path doesn´t exist. If i create the path manually, i get the exception, that the path already exists, so something's wrong with the If-Statement up in the Logging-class. But i don't know what the heck works wrong

Upvotes: 2

Views: 459

Answers (4)

er-sho
er-sho

Reputation: 9771

Try to take DirectoryPath and FilePath differently.

Move your StreamWriter to method scope so we can close this stream after Write content inside file.

public static class Logging
{
    public static string _DirectoryPath = $"C:\\Users\\{Environment.UserName}\\Desktop\\Logs";
    public static string _FileName = $"{DateTime.Now.ToString("dd.MM.yyyy")}.csv";

    public static void getPath(string path)
    {
        if (!Directory.Exists(path))
        {
            Directory.CreateDirectory(path);
        }
    }

    public static void logging(string message)
    {
        StreamWriter _sw = new StreamWriter(_DirectoryPath + "\\" + _FileName);
        _sw.Write(message);
        _sw.Flush();
        _sw.Close();
    }
}

And from Program.cs.

Logging.getPath(Logging._DirectoryPath);
Logging.logging("Test");

Output:

enter image description here

Upvotes: 1

Hassan Ata Ullah
Hassan Ata Ullah

Reputation: 357

Your path is a file and not a directory. You need to create the directory from your path

    String Path = $"C:\\Users\\{Environment.UserName}\\Desktop\\Logs\\{DateTime.Now.ToString("dd.MM.yyyy")}.csv";

    String Directory = System.IO.Path.GetDirectoryName(Path);

    if (System.IO.Directory.Exists(Directory)==false) {
      System.IO.Directory.CreateDirectory(Directory);
    }

    if (System.IO.File.Exists(Path)==false) {
      System.IO.File.Create(Path);
    }

Upvotes: 4

MindSwipe
MindSwipe

Reputation: 7855

Your testing if an Directory exists but your giving the path to a File. Here's some code you could use to fix it:

public static string _Path = $"C:\\Users\\{Environment.UserName}\\Desktop\\Logs";
public static string _Filename = $"{DateTime.Now.ToString("dd.MM.yyyy")}.csv";
static StreamWriter _File = new StreamWriter(_File);

Upvotes: 1

gofal3
gofal3

Reputation: 1238

Your _Path variable isn't actually a directory, but rather a filename.

You get the Directory with System.IO.Path.GetDirectoryName(_Path)

Upvotes: 1

Related Questions