Click here to Skip to main content
15,902,893 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
I have selected a file using open file dialog.

Then I need to add all the text contents in the file to a string variable.

How to do so.
Posted

You could just do this:
C#
string s = "my original content";
s += File.ReadAllText(path);

But that is pretty inefficient:
C#
string s = "my original content";
string fileData = File.ReadAllText(path);
StringBuilder sb = new StringBuilder(s.Length + fileData.Length);
sb.Append(s);
sb.Append(fileData);
s = sb.ToString();

Might be better, particularly if you are doing this a lot and can re-use the StringBuilder

"I am using open file dialog to select 5 files at runtime.

How to create five strings at runtime to store all text files in appropriate strings"


Try this:
C#
OpenFileDialog ofd = new OpenFileDialog();
ofd.Multiselect = true;
if (ofd.ShowDialog() == DialogResult.OK)
    {
    List<string> data = new List<string>();
    foreach (string file in ofd.FileNames)
        {
        data.Add(File.ReadAllText(file));
        }
    }
Or this if you prefer Linq:
C#
OpenFileDialog ofd = new OpenFileDialog();
ofd.Multiselect = true;
if (ofd.ShowDialog() == DialogResult.OK)
    {
    string[] data = ofd.FileNames.Select(f => File.ReadAllText(f)).ToArray();
    }
 
Share this answer
 
v2
Comments
KUMAR619 26-Mar-14 5:14am    
@OriginalGriff

I am using open file dialog to select 5 files at runtime.

How to create five strings at runtime to store all text files in appropriate strings
OriginalGriff 26-Mar-14 5:49am    
Answer updated.
KUMAR619 26-Mar-14 6:22am    
Thanks but I need to store in different files


such as data[0],data[1],data[2],data[3],data[4]

How to do this
KUMAR619 26-Mar-14 6:38am    
I have to store all files in different string such as
textFromFile[0],textFromFile[1],textFromFile[2],textFromFile[3]textFromFile[4]

How to do this

Please help me
OriginalGriff 26-Mar-14 6:47am    
Um.
Did you try it?
Because that is exactly what both of those code fragments do...
You can read the content from a text file as the follows:
C#
String yourFileFromDialog = "file location and file name";
String textFromFile = string.Empty;

try
{
   using (StreamReader sr = new StreamReader(yourFileFromDialog))
   {
       textFromFile = sr.ReadToEnd();
   }
}
catch (Exception e)
{
   //exception handling
}


Here[^] you can read more.

If you have a string array for your file names, do the follow:
C#
string[] fileNames = //fill up from your dialog

String yourFileFromDialog = string.Empty,
       textFromFile = string.Empty;

try
{
   for(int i = 0; i < fileNames.length; i++)
   {
      yourFileFromDialog = fileNames[i];
    
      using (StreamReader sr = new StreamReader(yourFileFromDialog))
      {
         textFromFile += sr.ReadToEnd();
      }     
   }

}
catch (Exception e)
{
   //exception handling
}


For the later usage of the text from the files you can make a private property to your class and append this instead of textFromFile variable:
C#
private string textFromFiles = string.Empty; 
 
Share this answer
 
v4
Comments
KUMAR619 26-Mar-14 5:14am    
@Norbitrial

I am using open file dialog to select 5 files at runtime.

How to create five strings at runtime to store all text files in appropriate strings
norbitrial 26-Mar-14 5:17am    
If you store your file names for example in a string array then you can iterate through that array and use the code what I've added as a solution. In each running of your iteration you can change the yourFileFromDialog variable value to the actual file name.
KUMAR619 26-Mar-14 5:18am    
Can you get me a code snippet for that please
norbitrial 26-Mar-14 5:25am    
I've improved my solution.
KUMAR619 26-Mar-14 5:48am    
Thanks
How to create individual string for all files so that I can use it for further purposes
Chk this code :

C#
 System.IO.StreamReader file = new System.IO.StreamReader(@"D:\test.txt");
String Line="";
String txtLine = "";
while ((Line = file.ReadLine()) != null)
{
    txtLine = txtLine + Line;
  
}
file.Close();
            textBox4.Text=txtLine;
 
Share this answer
 
Comments
KUMAR619 26-Mar-14 5:13am    
@Syed

I am using open file dialog to select 5 files at runtime.

How to create five strings at runtime to store all text files in appropriate strings
C#
StringBuilder sb = new StringBuilder();
using (StreamReader sr = new StreamReader("TestFile.txt"))
{
    String line;
    // Read and display lines from the file until the end of
    // the file is reached.
    while ((line = sr.ReadLine()) != null)
    {
        sb.AppendLine(line);
    }
}
string allines = sb.ToString();



Try Also this Link
 
Share this answer
 
v2
Comments
KUMAR619 26-Mar-14 5:13am    
@NandaKishore

I am using open file dialog to select 5 files at runtime.

How to create five strings at runtime to store all text files in appropriate strings
nandakishoreroyal 26-Mar-14 5:26am    
create string array, and using for loop assign text in to that..

paste your code, i will write the condition...

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