paddy
paddy

Reputation: 165

StreamReader Could not find file (Because it is not being created)

I am trying to create a file for the further write to and read from.

I use Directory.CreateDirectory and File.Create but neither path nor file are being created.

On the Page which part I show here below I check if File exists, and if not, I create a File. On Second Page (that I dont show here) I add new lines to the file using StreamWrite. When saved, first Page comes to focus again and lists the content of the File(only one row in this study).

Here is my code for the part in question:

public async Task ReadFileAsync()
    {


        string directoryName = Path.GetDirectoryName(@"C:\Users\...\DataBase\");
        Task.Run(async() => Directory.CreateDirectory(directoryName));
        Task.Run(async() => File.Create(directoryName + "ProductsDatabase.txt"));

        //code for reading from file
        string path = (directoryName + "ProductsDatabase.txt");

        using (StreamReader ProductsDatabaseRead = new StreamReader(File.OpenRead(path)))
        {
            ProductOneTextBlock.Text = ProductsDatabaseRead.ReadLine();

        }
        if (ProductOneTextBlock.Text == "")
            {
            ProductOneTextBlock.Text = "Nothing to show";
            }
    }

The file and folder are not being created.

I don't get any error.

I tried also different folders on the drive in case if there was READ ONLY folder in solution folder. No difference.

Anyone could help? (I found many threads about this problem but here I cannot resolve it with none of the solutions.

Physical file is not being created.

When I attempt to write to it (from another page) I get error that the file could not be found(because it is not there indeed).

It seems that program loses itself somewhere between

Task.Run(async() => Directory.CreateDirectory(directoryName));
        Task.Run(async() => File.Create(directoryName + "ProductsDatabase.txt"));

and:

using (StreamReader ProductsDatabaseRead = new StreamReader(File.OpenRead(path)))
        {
            ProductOneTextBlock.Text = ProductsDatabaseRead.ReadLine();

        }

, as TextBlock is not being updated even if ProductsDatabaseRead is null.

If I put

ProductOneTextBlock.Text = "Nothing to show";

a the begining of the method, TextBlock gets updated.

SO, why the

using (StreamReader ProductsDatabaseRead = new StreamReader(File.OpenRead(path)))

does not work?

Upvotes: 1

Views: 2426

Answers (2)

Lucas
Lucas

Reputation: 441

public void ReadFile()
{
    string folderPath = @"C:\Users\patri\source\repos\DietMate\";
    string fileName = "ProductsDatabase.txt";
    string fullPath = Path.Combine(folderPath, fileName);

    //insert code to check whether file exists.
    // use Exists()    
    if (!Directory.Exists(folderPath))
    {
        Directory.CreateDirectory(folderPath);
        File.Create(fullPath);
    }

    //if yes, follow with code below
    //insert code for reading from file
    using (StreamReader ProductsDatabaseRead = new StreamReader(fullPath))
    {
        ProductTest.Text = ProductsDatabaseRead.ReadLine();
    }
}

Upvotes: 1

You're not waiting for Task.Run to complete. Your directory creation, file creation and attempt to open a "as you think newly created file" are out of order. That's why you're probably not able to open a file (it still does not exist at this point).

Task.Run returns a task that will be completed when the work is done. You need to wait for completion.

Upvotes: 4

Related Questions