SilverLight C#程序之:把二进制数据流转换为文件 并打开文件及exe程序

概述

上节我们了解了 将文件转换为二进制数据流 并保存到数据库中。。。

这节我们来看看 怎么将这些二进制流转换为文件 并打开他。。。

点击查看之后(此数据为保存到数据库中的数据)

SilverLight代码

View Code
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="30"></RowDefinition>
</Grid.RowDefinitions>
<sdk:DataGrid x:Name="gridConAttachmentModel" Grid.Row="0" RowHeight="25" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="#FEFEFE" HeadersVisibility="Column" BorderThickness="0,1,0,0" GridLinesVisibility="All" AutoGenerateColumns="False" FrozenColumnCount="1" AreRowDetailsFrozen="True"
ItemsSource
="{Binding ConAttachmentModelList}" SelectedItem="{Binding ConAttachmentModel}" IsReadOnly="True">
<sdk:DataGrid.Columns>
<sdk:DataGridTextColumn Header="序号" Binding="{Binding ConAtt_OrderNum,Mode=TwoWay}" IsReadOnly="True" />
<sdk:DataGridTextColumn Header="附件名称" Binding="{Binding ConAtt_Name,Mode=TwoWay}" />
<sdk:DataGridTextColumn Header="附件资料" Binding="{Binding ConAtt_Name,Mode=TwoWay}">
</sdk:DataGridTextColumn>
<sdk:DataGridTextColumn Header="操作员编号" Binding="{Binding ConAtt_MakerId,Mode=TwoWay}" IsReadOnly="True" Visibility="Collapsed"></sdk:DataGridTextColumn>
<sdk:DataGridTextColumn Header="操作员名称" Binding="{Binding ConAtt_MakerName,Mode=TwoWay}" IsReadOnly="True" />
<sdk:DataGridTextColumn Header="备注" Binding="{Binding ConAtt_Remark,Mode=TwoWay}" />
<sdk:DataGridTemplateColumn Header="预览" Width="90" >
<sdk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<HyperlinkButton Content="查看" Margin="5" Click="HyperlinkButton_Click"></HyperlinkButton>
</DataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>
</sdk:DataGridTemplateColumn>
</sdk:DataGrid.Columns>
</sdk:DataGrid>
<Button Height="20" Width="80" Margin="5" Content="添加文件" Grid.Row="1" HorizontalAlignment="Left" Command="{Binding UploadCommd}">
</Button>
</Grid>

cs代码

View Code
private void HyperlinkButton_Click(object sender, RoutedEventArgs e)
{
//获取当前选择行的实体对象
var conAttachmentModel = gridConAttachmentModel.SelectedItem as ConAttachmentModel;
if (conAttachmentModel != null)
{
//如果不为空 把获取到的对象作为参数传入 web端 用来打开文件
RequestService.Sent(ContractManageUriNames.OpenContractFiles, conAttachmentModel,
delegate(object sent, ResponseArgs args) { });
}
}

//打开文件
public void OpenContractFiles(ConAttachmentModel conAttachmentModel)
{
//获取文件类型
// var filesType = conAttachmentModel.ConAtt_Name.Substring(conAttachmentModel.ConAtt_Name.LastIndexOf("."));
//获取二进制流文件
var filebyte = conAttachmentModel.ConAtt_Files;
//创建的文件路径
var path = @"C:\Documents and Settings\Administrator\Local Settings\Temp\\" + conAttachmentModel.ConAtt_Name;
//按照路径实例化文件
var file = new FileStream(path, FileMode.Create, FileAccess.Write);
//实例化一个用于写的 BinaryWriter
var bw = new BinaryWriter(file);
//将传进来的二进制字符转换为8位无符号整数数组再写进去
bw.Write(filebyte);
bw.Close();
//关闭二进制流写入器
file.Close(); //关闭文件流

//创建Process命令
var cmd = new Process();
//创建要运行的文件或者程序
var startfile = new ProcessStartInfo
{
FileName
= file.Name,//文件完全路径
WindowStyle = ProcessWindowStyle.Normal,//Windows窗口样式
UseShellExecute = true//为true,则用默认的打开方式打开,如果是exe等,设置为false
};
cmd.StartInfo
= startfile;
cmd.Start();
//打开文件
}

大功告成!!!


作者:记忆逝去的青春
出处:http://www.cnblogs.com/lukun/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,如有问题,可以通过http://www.cnblogs.com/lukun/  联系我,非常感谢。

posted on 2011-04-12 14:33  记忆逝去的青春  阅读(2883)  评论(3编辑  收藏  举报