1

I retrieved the data that I saved in a filestream column as an example the below code, and got it into memory stream or byte array. How can I open it with its default?

OS application like opening a photo with photo manager or word document with MS Word ? Normally I use the Process.Start command but it doesn't work with memorystream or byte array so how can this be done ?

 public DataTable RetriveFile(int Code)
        {
            mydbms d = new mydbms();
            DataTable dt;


           String com2 = "select * from Matn_Naame where Code=" + Code + ";";
           dt = d.executeselectsql(com2);


            return dt;
        }
{
Letter_Manager L_M = new Letter_Manager();
            Byte[] b;
            DataTable dt = new DataTable();
            dt = L_M.RetriveFile(6);

            b=(Byte[])dt.Rows[0]["fileContent"];
            path = dt.Rows[0]["Path"].ToString();

                 MemoryStream Memory = new MemoryStream(b);
                 Memory.Write(b, 0, b.Length);
//??????????????????**?for open and show file**
}

2 Answers 2

4

You will need to save the data to a temporary file. A temporary filename is available by calling System.IO.Path.GetTempFileName(). You can then simply use a FileStream to save to that path, and as you mentioned, Process.Start to launch the default application.

Sign up to request clarification or add additional context in comments.

5 Comments

Please Help i have blow code: FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write); Path.GetTempFileName(); fs.Write(b, 0, b.Length); fs.Flush(); fs.Close(); i dont now satrt fs if Process.Start(fs.Name)? but it start app with same path that exit not from sql
but it start file with same path that exist not from read byte from sql
This is a very useful answer. I didn't even know the existance of such a method. Thanks!
@Mary: You should be assigning path = Path.GetTempFileName(); and then use Process.Start(path).
@Mary: If you have everything working now, would you mind accepting the answer?
1

The easiest way is to use the convenient File.WriteAllBytes method which writes a byte array directly to the file name specified.

var fileName = Path.ChangeExtension(Path.GetTempFileName(), "txt");
File.WriteAllBytes(fileName, b);
Process.Start(fileName);

Note that in my example I have assumed that you are dealing with plain text data that you wish to open with the default text editor.

Windows will not know what application to launch this file unless you give it a meaningful extension. You either have to know the format of the data upfront so you can append the correct extension or you'll need to use a library that can guess what kind of data it is such as the excellent TrID file identification tool.

6 Comments

thanx for answer but when run this codes one temp file with tmp extention creat and error occurd: No application is associated with the specified file for this operation please help me thanx
See the amendment to my answer regarding file extensions.
my extetention files is txt or doc or docx where i append my extetion to tempfile?
I have changed the code so that the temp file extension is "txt". You still need some way to determine if you're dealing with plain text or an MS Word document.
very thanx for your HElp Thanxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx :-)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.