how to read a text file dynamically?

hi I want to read a text file without giving pathname which may be sav

Page 1 of 1

5 Replies - 2799 Views - Last Post: 30 January 2009 - 10:15 AM Rate Topic: -----

#1 priks_24  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 28-January 09

how to read a text file dynamically?

Post icon  Posted 28 January 2009 - 11:13 PM

hi I want to read a text file without giving pathname which may be saved in any drive.
Is This A Good Question/Topic? 0
  • +

Replies To: how to read a text file dynamically?

#2 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1641
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: how to read a text file dynamically?

Posted 28 January 2009 - 11:26 PM

Well a path is required, how else if it going to know where the file is?
Was This Post Helpful? 0
  • +
  • -

#3 priks_24  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 28-January 09

Re: how to read a text file dynamically?

Posted 28 January 2009 - 11:37 PM

thanks for reply but i want to give only file name not to give pathname.is it possible in this way? Can i have to first search the file hence i should read the file.

This post has been edited by priks_24: 28 January 2009 - 11:52 PM

Was This Post Helpful? 0
  • +
  • -

#4 Kirth  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 157
  • Joined: 09-July 08

Re: how to read a text file dynamically?

Posted 29 January 2009 - 05:14 AM

If you're doing it like:
string path = Console.ReadLine();
StreamReader strRdr = new StreamReader(path);



You could for example fill in "File.php" and it'll read from the file "File.php" in the same path as the executable.

Or:

string path = Console.ReadLine();
StreamReader strRdr = new StreamReader("files/" + path);



Will read the file "File.php" (or any other inserted file) in the folder "files".

I hope this somewhat solved your question.
Was This Post Helpful? 0
  • +
  • -

#5 priks_24  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 28-January 09

Re: how to read a text file dynamically?

Posted 30 January 2009 - 05:54 AM

Thanks Kirth,

my coding is


try
			{
				using (StreamReader sr = new StreamReader("C:/Documents and Settings/abc/Desktop/connection.txt"))
				{
					String line;
					while ((line = sr.ReadLine()) != null)

					{
						Console.WriteLine(line);
						listBox1.Items.Add(line);
					}				   
				}
			}
			catch (Exception e)
			{
				 Console.WriteLine("The file could not be read:");
				 Console.WriteLine(e.Message);
			 }


how can i read this text file without giving path i mean dynamically.

This post has been edited by Jayman: 30 January 2009 - 10:13 AM

Was This Post Helpful? 0
  • +
  • -

#6 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: how to read a text file dynamically?

Posted 30 January 2009 - 10:15 AM

Store the file in the same folder as the executable program. Then you will not need to supply the path to the file, you can simply reference it by name and extension.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1