Click here to Skip to main content
16,010,334 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
Hi
I am trying to open an attached file. When i click on nothing happens. Any help would be appreciated.

Regards
George

Code :
C#
protected void Attached_SelectedIndexChanged(object sender, EventArgs e)
        {
            String FileName = Attached.SelectedValue.ToString();
            //FileStream fileStream = new FileStream(@FileName, FileMode.Open);
            try
            {
                FileStream fileStream = new FileStream(@FileName, FileMode.Open, FileAccess.ReadWrite);
                int length = (int)fileStream.Length;  // get file length
                buffer = new byte[length];            // create buffer
                int count;                            // actual number of bytes read
                int sum = 0;                          // total number of bytes read

                // read until Read method returns 0 (end of the stream has been reached)
                while ((count = fileStream.Read(buffer, sum, length - sum)) > 0)
                    sum += count;  // sum is a buffer offset for next reading

            }
            finally
            {
                fileStream.Close();
            }

            //System.Diagnostics.Process.Start(@FileName);
        }
Posted
Updated 11-Jun-11 2:45am
v3
Comments
Sandeep Mewara 10-Jun-11 11:34am    
Always wrap your code with PRE tag. It makes the code part readable.
J a a n s 10-Jun-11 12:06pm    
1. Where is the code for opening the attachment?
2. What is the type of 'Attached'?

Nothing happens… How do you know? You do nothing, just read the file and apparently discard the result. Use debugger to see if this code is even called. If it is not, put a break point on the method which is supposed to call this method, and so on, until you find all the ends.

Keep it simple. Use the class System.IO.StreamReader (you're not writing anything). You don't have to use try-finally (which is correct), because using statement does it all for you:

C#
using System.IO;

//...

using (StreamReader reader = new StreamReader(fileName, true)) {
    string content = reader.ReadToEnd(); //there is an option to read it all at once
    // return content; it's good if the content is a string
    // ..if not, read it the way you want; don't forget to return the content
} // reader is disposed here


—SA
 
Share this answer
 
I hope, this will help you...

C#
protected void Attached_SelectedIndexChanged(object sender, EventArgs e)
{
    String FileName = Attached.SelectedValue.ToString();
    try
    {
      using (FileStream fileStream=new FileStream(@FileName,FileMode.Open, FileAccess.ReadWrite))
      {
            int length = (int)fileStream.Length;  // get file length
            buffer = new byte[length];            // create buffer
            int count;                            // actual number of bytes read
            int sum = 0;                          // total number of bytes read

            // read until Read method returns 0 (end of the stream has been reached)
            while ((count = fileStream.Read(buffer, sum, length - sum)) > 0)
                sum += count;  // sum is a buffer offset for next reading

      }
      System.Diagnostics.Process.Start(@FileName);
    }
    catch (Exception ex)
    {
        MessageBox.Show("Error : "+ex.Message);
    }
}
 
Share this answer
 
Comments
Gthomas 24-Aug-11 5:28am    
Error in step: buffer = new byte[length]; // create buffer
and while ((count = fileStream.Read(buffer, sum, length - sum)) > 0)
in the above solution(solution2)

Regards

George
Gthomas 24-Aug-11 7:35am    
Using the solution 1 or 2 it tries to open on the server. I want to open the file from client.

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