简体   繁体   中英

File not visible while reading and writing in C# to notepad

I am writing a sample text to notepad and reading the same in C#

 try{
                StreamWriter sw = new StreamWriter(path);
                DateTime d = new DateTime();
                sw.WriteLine("The time is " + d);
                sw.Close();
                Console.WriteLine("Reading file contents...");
                StreamReader sr = new StreamReader(path);
                string line;
                line = sr.ReadLine();
                while (line != null)
                {
                    Console.WriteLine(line);
                    line = sr.ReadLine();
                }
                sr.Close();
               }
             catch(Exception e)
             {
               Console.WriteLine(e.Message);
             }

The code executes fine and I can see the output on the screen.

However, the file is not visible in the path location.

This is a guess, but my bet is that you have a relative path, and that the program is not located in the location you think it is, thus you're looking for the file in the wrong place. If this is under visual studio, make sure you look for the file in the debug folder of your project.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM