1

I initially have a Fileupload tool to upload a textfile, manipulate its content and display into a Listbox or Textbox. The limitation however is Fileupload only supports single uploading, at least to the version of .Net Framework I am using.

What I intend to do is just use a button control and remove the Fileupload. Upon Button click I need to read the textfiles inside a designated folder path and display first the contents inside a multiple lined textbox. (not just the file name) This is my intially written codes, and it is not working.

   protected void btnGetFiles_Click(object sender, EventArgs e)
        {
            string content = string.Empty;
            DirectoryInfo dinfo = new DirectoryInfo(@"C:\samplePath");
            FileInfo[] Files = dinfo.GetFiles("*.txt");



            foreach (FileInfo file in Files)
            {
                //ListBox1.Items.Add(file.Name);
                content += content;

            }
            txtContent.Text = content;


        }
sujith karivelil
  • 28,671
  • 6
  • 55
  • 88
rickyProgrammer
  • 1,177
  • 4
  • 27
  • 63
  • Just an FYI, the .NET framework has nothing to do with multiple files being uploaded. This is pure client/IIS at work. To see how to allow multiple file uploads, look at [this SO question](http://stackoverflow.com/questions/17441925/how-to-choose-multiple-files-using-file-upload-control) – Icemanind Feb 13 '17 at 03:09
  • Because i red somewhere that Fileupload tool can have a multipleUpload feature in the latest version. Thanks though for the correction – rickyProgrammer Feb 13 '17 at 03:11

2 Answers2

1

Since your's is web based application you can't access physical paths like c:\\.. you should use Server.MapPath anyway(As per the comment, you don't need to get the file with Server.MapPath). Then for getting the content you can try something like the following:

  protected void btnGetFiles_Click(object sender, EventArgs e)
  {
      try
      {
          StringBuilder content = new StringBuilder();
          if (Directory.Exists(@"C:\samplePath"))
          { 
              // Execute this if the directory exists
              foreach (string file in Directory.GetFiles(@"C:\samplePath","*.txt"))
              {
                   // Iterates through the files of type txt in the directories
                  content.Append(File.ReadAllText(file)); // gives you the conent
              }
                txtContent.Text = content.ToString();
          }
      }
      catch
      {
          txtContent.Text = "Something went wrong";
      }

  }
sujith karivelil
  • 28,671
  • 6
  • 55
  • 88
  • actually sir, the application is for local use for now, so it will be okay if I am using absolute path. The current code can actually locate the path the only problem is only the content of the files are not being read. Only the file name. When I try the code, expected that I am having an error that a virtual path must be encoded. – rickyProgrammer Feb 13 '17 at 03:24
  • @rickyProgrammer: ok then go ahead with `"C:\samplePath"` instead for `Server.MapPath("Relative path here")` and check whether it is working; See the updated code – sujith karivelil Feb 13 '17 at 03:26
  • Thanks what if I like it to be displayed in gridview? – rickyProgrammer Feb 13 '17 at 03:35
  • You can get the content into a List or dataTable and then bind the grid with that collection – sujith karivelil Feb 13 '17 at 03:54
0

you wrote content += content;, that is the problem. change it to content += file.Name;, it will work.