-6

I'm trying to create a method that checks if this file ( todayfile.txt) is created if not I need it to create it. Here is what I thinking of :

private void ReadWater()
    {
        try
        {
            StreamReader inputFile;

            // I want to check if there is a file named ( Todayfile.txt )

            if (// if this file ( Todayfile.txt) is founded)
            {
                // Do this
            }

            else // if there is no file in this nmae ( Todayfile.text )
            {
                // create a new file 
            }

        }
        catch (Exception ex)
        {

        }

    }
Riyad
  • 1
  • 1
  • 3
  • 6
    Try `File.Exists(path)`. – Enigmativity Apr 01 '16 at 22:51
  • 3
    And please don't do `catch (Exception ex)` - that's a bad practice and just hides errors. If you think an exception can be thrown and that you can **handle it meaningfully** then catch **that specific exception only**. – Enigmativity Apr 01 '16 at 22:53
  • 3
    Copying and pasting the title into Google returns `File.Exists`. – 001 Apr 01 '16 at 22:54
  • Enigmativity is right. File.Exist(path) will return a bool == true if the file exists. I am quite surprised you couldn't find this question elsewhere without having to post a question. There should be plenty of documentation out there using a simple google search. One thing you should know is you have to use the full path name. Also, you need to escape the slashes by doing a double slash or you can just add an "@" symbol at the beginning of the string which will tell it to treat escape characters literally. – JSON Apr 01 '16 at 23:02
  • 1
    As noted by others, this answer can easily be found online with a simple search. – gxclarke Apr 01 '16 at 23:22

1 Answers1

0

You can use the System.IO.File class to check for and create the file.

The following example demonstrates how to use the File class to check whether a file exists, and depending on the result, either create a new file and write to it, or open the existing file and read from it.

private void ReadWater()
{
    string path = "Todayfile.txt";
    // if there is no file in this name ( Todayfile.txt )
    if(!System.IO.File.Exists(path)) {
        // Create a file to write to.
        using (StreamWriter sw = File.CreateText(path)) {
            sw.WriteLine("Hello");
            sw.WriteLine("And");
            sw.WriteLine("Welcome");
        }
    }
    //at this point file should exist.

    // Open the file to read from.
    using (StreamReader sr = File.OpenText(path)) {
        string s = "";
        while ((s = sr.ReadLine()) != null) {
            Console.WriteLine(s);
        }
    } 
}

check the links provided above to get a more detailed explanation of the System.IO.File class and its methods.

Nkosi
  • 235,767
  • 35
  • 427
  • 472
  • 1
    @Upvoters, how do you think this code works.. One condition opens for write, and the other for read. If you were the coder, how would you use that `fileStream` BTW: `File.Exists` is suggested 15 mins before that answer which doesn't add any value to that comment.... – Eser Apr 01 '16 at 23:14
  • `File.Exists` was only part of the answer being sought by the OP. – Nkosi Apr 01 '16 at 23:26
  • Your code is still buggy, You lost the file pointer after `System.IO.File.Create(path);` You can not write to this file anymore since it is still open :) – Eser Apr 01 '16 at 23:26
  • 2
    Nkosi, this is your answer... I only say your code is not correct.... How to correct is your problem. If you don't know, don't post an answer..... – Eser Apr 01 '16 at 23:29
  • @Eser noted and understood. just trying to help. which is what I thought the site was all about. – Nkosi Apr 01 '16 at 23:32
  • Helping can only be done with correct answers..... – Eser Apr 01 '16 at 23:33
  • @Eser, you are correct. – Nkosi Apr 01 '16 at 23:33
  • And for your last edit, what if the file *exists*. Just repeat the code in `// Add some information to the file.` part? – Eser Apr 01 '16 at 23:37
  • The OP only stated he wanted to check if the file existed and if not create it. So i stopped there. – Nkosi Apr 01 '16 at 23:39
  • A good example for how a simple problem can get quite complex with a bad coding.... – Eser Apr 01 '16 at 23:39
  • noted also:) not to get rude as most of SO users do when someone criticizes their answer. – Eser Apr 01 '16 at 23:45
  • I wasn't being rude. If it came across so then that was not my intention. – Nkosi Apr 01 '16 at 23:47
  • No I didn't say that. In fact, I thank to you for it.. This is not a common ( polite ) response for me when I write a comment for answers.. – Eser Apr 01 '16 at 23:55