Click here to Skip to main content
15,907,395 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Let say in a form,I select which file from the desktop is to be read through menu strip.but how to send directory of that file to the class so that the class know which file is to be read?The class already have all the readtext code.thanks.
Posted
Updated 19-Jul-12 23:38pm
v2
Comments
dimpledevani 20-Jul-12 5:38am    
If you are working on web then use FileUpload Control or you must use FileDialog box control to select and specify your required file
Esmond90 20-Jul-12 5:47am    
Thanks for reply.Ya,i use FileDialog box.But how the code should be written at directory path for the class?

eg. class: System.IO.StreamReader sr = new System.IO.StreamReader(DIRECTORY PATH)

Form: DialogResult result = this.openFileDialog1.ShowDialog();

if (result == DialogResult.OK) { string file = openFileDialog1.FileName;

}
Sunny_Kumar_ 20-Jul-12 5:52am    
Initialize the streamreader with a filestream like this:
StreamReader sr = new StreamReader(new FileStream("filepath", FileMode.Open));

As I know, StreamReader never takes a Directory as an argument.
replace "filepath" with the actual filename which you're selecting using OpenFileDialog.
Esmond90 20-Jul-12 6:00am    
Ok.Thanks for the information.I am trying on it,let you guys know when I know what actually the problem is.Thanks!

1 solution

Hi Esmond,

Well, first I would like to let you know that if you're using OpenFileDialog to select a file, it returns the full path of selected file or files, So you won't need at all to indicate the drive to search a file. Just pass the complete file name which is absolute. If you're using any other mean to select a file then please specify that in your question using "Improve question" link in your post so that we can get a better picture of your problem.



You can parse contents from a text file like this:

string DataInTextFile = "";

            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "Text Files|*.txt";
            DialogResult dr = ofd.ShowDialog();
            if (dr == DialogResult.OK)
            {
                StreamReader SR = new StreamReader(ofd.FileName);
                DataInTextFile = SR.ReadToEnd();
                SR.Close();
                SR.Dispose();
            }

            MessageBox.Show(DataInTextFile);


Go ahead with the manipulation on text file contents using "DataInTextFile" string object.

Hope this helps.

Happy Coding :)
 
Share this answer
 
v3
Comments
Esmond90 23-Jul-12 1:50am    
Hello..My problem is i open a text file and i want to pass it as an object to the class to undergo process to get the data i want in array..so i hav no idea after i choose the file,how it is to be used for the process in the class..?
Sunny_Kumar_ 23-Jul-12 2:04am    
please be specific and clear what you have to achieve. you have to fetch a text file words in an array of something else. If you need to fetch contents of a text file then you should use StreamReader Object. Please specify what you wanna do & then I'll of help to you.
Esmond90 23-Jul-12 4:07am    
Ok.I want to choose a specific file to be read.I don't know how to make the program know that which file is chosen.I mean to specify the filepath.thanks!
Sunny_Kumar_ 23-Jul-12 5:57am    
I've updated the answer. Please upvote and acept as answer if tt helps you. Thanks.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900