如何打开一个字节数组返回到一个文件中,并用C#自动打开呢? [英] How do I turn an array of bytes back into a file and open it automatically with C#?

查看:183
本文介绍了如何打开一个字节数组返回到一个文件中,并用C#自动打开呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写一些code添加文件附件到应用程序我建立。

I am writing some code to add file attachments into an application I am building.

我有加和放大器;删除工作,但我不知道从哪里开始实行开放。

I have add & Remove working but I don't know where to start to implement open.

我有一个字节数组(从表中字段),我不知道如何使它自动打开如

I have an array of bytes (from a table field) and I don't know how to make it automatically open e.g.

如果我有一个字节数组,它是PDF,如何让我的应用程序来自动打开Acrobat或任何当前指定的应用程序的扩展名是用C#?

If I have an array of bytes which is a PDF, how do I get my app to automatically open Acrobat or whatever the currently assigned application for the extension is using C#?

推荐答案

为了在任何外部应用程序中打开它,你需要写字节到磁盘,然后使用的Process.Start发动对临时文件关联的应用程序。刚刚通过的临时文件名(具有相应的扩展名)作为唯一的参数的的Process.Start,它会在相应的应用程​​序打开该文件。

In order to open it in any external application, you'll need to write the bytes to disk, then use Process.Start to launch the associated application on the temporary file. Just pass the temporary filename (with the appropriate extension) as the only argument the Process.Start, and it will open that file in the appropriate application.

某些应用可能有一种方法来喂字节流,但是这将需要通过明确目标应用程序进行处理。

Some applications may have a way to feed a stream of bytes, but this would need to be handled by the target application explicitly.

有关样本code,你可以这样做:

For sample code, you could do something like:

byte[] filedata = GetMyByteArray();
string extension = GetTheExtension(); // "pdf", etc

string filename =System.IO.Path.GetTempFileName() + "." + extension; // Makes something like "C:\Temp\blah.tmp.pdf"

File.WriteAllBytes(filename, filedata);

var process = Process.Start(filename);
// Clean up our temporary file...
process.Exited += (s,e) => System.IO.File.Delete(filename); 

这篇关于如何打开一个字节数组返回到一个文件中,并用C#自动打开呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆