In a Silverlight 4 OOB App (even with elevated trust), you cannot access files in locations outside of 'MyDocuments' and a couple of other user related special folders. You have to use the workaround 'COM+ Automation'. But the Scripting.FileSystemObject, which works great for text files, cannot handle binary files. Fortunately you can also use ADODB.Stream there. And that handles binary files just fine. Here is my code, tested with Word Templates, .dotx files:
public static void WriteBinaryFile(string fileName, byte[] binary)
{
const int adTypeBinary = 1;
const int adSaveCreateOverWrite = 2;
using (dynamic adoCom = AutomationFactory.CreateObject("ADODB.Stream"))
{
adoCom.Type = adTypeBinary;
adoCom.Open();
adoCom.Write(binary);
adoCom.SaveToFile(fileName, adSaveCreateOverWrite);
}
}
A file read can be done like this:
public static byte[] ReadBinaryFile(string fileName)
{
const int adTypeBinary = 1;
using (dynamic adoCom = AutomationFactory.CreateObject("ADODB.Stream"))
{
adoCom.Type = adTypeBinary;
adoCom.Open();
adoCom.LoadFromFile(fileName);
return adoCom.Read();
}
}
分享到:
相关推荐
使用vbs读写文件(Adodb.Stream)方式,防止乱码
vb adodb stream aeecss adodb resouse recordset
adodb.stream 模仿sina的天气预报
Adodb.Stream是ADO的Stream对象,提供存取二进制数据或者文本流,从而实现对流的读、写和管理等操作及各种方法的使用。 http://www.abo518.cn
adodb.stream
主要介绍了adodb.recordset.open(rs.open)方法参数详解,需要的朋友可以参考下
ADODB.Stream组件Charset属性值集合
卸载/恢复ADODB.Stream的软件
代码如下:‘函数名称:ReadTextFile ‘作用:利用AdoDb.Stream对象来读取UTF-8格式的文本文件 ‘—————————————————- Function ReadFromTextFile (FileUrl,CharSet) If FileUrl = “” OR Is...
用ADODB.Stream转换
asp adodb.stream对象的方法/属性
asp之基于adodb.stream的文件操作类
在ASP中,我们常用Scripting.FileSystemObject组件来读写文本文件,但它有一个缺点是对编码支持不足,比如我们想读写一个utf-8文本文件,FSO就很难办到了,这时我们就需要使用Adodb.Stream组件来代替它。
尝试过 防盗链,不太理想,最终使用了 Adodb.Stream 读取,直接输出。 解决了 盗版的问题,但是新的问题又来了。Adodb.Stream 这种方式 电脑还好说,大部分电脑浏览器都支持。移动端 很多 浏览器为了 加速读取,会多...
其中的ADODB.Connection中的连接字符串怎么写
用 XMLDOM 和 ADODB.Stream 实现base64编码解码 </title> <meta http-equiv=”Content-Type” content=”text/html; charset=gb2312″> <style type=”text/css” title=””> #textarea1,#...
———-‘—————利用xmlhttp和adodb.stream—————–‘On Error Resume Next‘——————————-定义输出格式—————————–path=request(“path”)if path =”” thenpath=”...
在浏览器的地址栏里直接输入一个doc或xls或jpg的文件的url路径,那么该文件会直接显示在浏览器里。而在很多时候我们希望能直接弹出下载提示框让用户下载,我们该怎么办呢