Solved

C# Class issue

Posted on 2014-12-22
2
171 Views
Last Modified: 2014-12-23
Hi see code below, i like to know this is write way to do that. code is working fine, but i feel something is not right.

My purpose is this code is  - in the text file has 3 lines, i need to access this 3 file  one by one.and need to put 3 variables.

see below code
string line = string.Empty;
Class ClientFiles
{

public  List<string []>  ClientSaveFileDetails()
{
List<string []> clientFileList = new List<string []>();
SaveFilename=@"C:\test.txt";
   using (StreamReader clientRead = new StreamReader(SaveFilename))
            {
                while ((line = clientRead.ReadLine()) != null)
                {
                    clientFileList.Add(new string[] { line });
                }
            }

            return clientFileList;
}



}


I have already called the above class using below code

ClientFiles cl=new ClientFiles();
List<string[]> clSitename = cl.ClientSaveFileDetails();


string rowdetail1=clSitename[0][0];//1st line of the file
string rowdetail2=clSitename[1][0];//2st line of the file
string rowdetail3=clSitename[2][0];//3st line of the file


Console.writeline(rowdetail1);

Console.writeline(rowdetail2);


Console.writeline(rowdetail3);

Open in new window


can some one show me its not right way? if yes please show me what is the best way to do that.thx
0
Comment
Question by:ukerandi
2 Comments
 
LVL 23

Assisted Solution

by:Michael74
Michael74 earned 250 total points
ID: 40514280
Not sure why you are using a string array for the lines of the file, a string variable will work just fine. Other than this I would also tend to declare the file name as an class variable to make editing it easier.

Class ClientFiles
{
   private string saveFileName = @"C:\test.txt";
   
   public  List<string>  ClientSaveFileDetails()
   {
      List<string> clientFileList = new List<string>();
      using (StreamReader clientRead = new StreamReader(saveFilename))
      {
             while ((line = clientRead.ReadLine()) != null)
             {
                 clientFileList.Add(line);
             }
       }

        return clientFileList;
   }
}

Open in new window

0
 
LVL 35

Accepted Solution

by:
Miguel Oz earned 250 total points
ID: 40514342
A simple approach to access those strings, use  the  File.ReadAllLines method:

public  List<string>  ClientSaveFileDetails(string saveFileName)
{
  string[] readText = File.ReadAllLines(saveFileName);
  return new new List<string>(readText);
}

Open in new window

Note: There is no need to use StreamReader.
Usage:
ClientFiles cl=new ClientFiles();
List<string> clSitename = cl.ClientSaveFileDetails(@"C:\test.txt");
//Your existing code goes here

Open in new window

0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
c# code 19 59
dynamic menu in asp.net c# 11 29
VB.Net - For Loop Error 5 26
Protecting vb6 & .Net code Obfuscation 18 51
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now