Trying to read from a text file

Govind Sankar

Active member
Joined
May 15, 2020
Messages
42
Programming Experience
Beginner
I have attached the c# project Excel Export. The first part of the project is reading from a text file using a stream reader and storing it in a sting file and displaying it using console.writeline. I tried doing this by watching a youtube video. But when I run the
program nothing is happening. I can see its written Exception thrown: 'System.IO.IOException' in System.Private.CoreLib.dll. What is the problem. Can anyone help me. You can go through the project. Its a very small one. Thank you.

This is the class

C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;

namespace ExcelExport
{
    class TextReader
    {
        string filePath = @"\\\\10.10.10.101\\kpg data\\Transfer\\Govind\\Cell Block Production Improvement\\Cell Block Production Project\\Phase 1\\Project Data\\Type of Data to be saved in Database.";
        string DataInsideText = "";
        StreamReader reader = null;
        
        public string readDataInsideText()
        {
            try
            {
                reader = new StreamReader(filePath);
                DataInsideText = reader.ReadLine();
                while(DataInsideText != null)
                {
                    Console.WriteLine(DataInsideText);
                    DataInsideText = reader.ReadLine();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }           
            return DataInsideText;
        }
    }
    
}

This is the end of the class

This is the beginning of the main Program

C#:
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
namespace ExcelExport
{   
    internal class Program
    {
        
        static void Main(string[] args)
        {
            Console.WriteLine("Hello");
            TextReader text = new TextReader();
            string data = text.readDataInsideText();
            Console.WriteLine(data);
        }
    }
}

This is the end of the main Program
 

Attachments

  • ExcelExport.zip
    268.6 KB · Views: 16
If you read more the exception message, it should tell you more about the error. Furthermore, if you were actually running your code in the debugger, you could step through the code and see those additional details regarding the exception as well.

As an aside, only implement try-catch if you intend to actually handle the exception that you are catching. To me your handling looks very inadequate. From what I see, you catch any kind of exception, print it out, and then you go merrily along aa of nothing had happened. So if the file doesn't exists or won't open, you are treating it as the same as the file does exist and it just happens to be empty. What if the exception is that you are out of memory, or if there is a stack overflow? Did you do something to resolve that kind of exception?

Anyway, on line 12, you are using the @ string prefix. When you use that, there is no need to escape the backslashes like you are currently doing. Also, it looks like the file you are trying to open does not have file extension. Is this intended?
 

Latest posts

Back
Top Bottom