• 文件转string,string转换成文件


    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;

    namespace DFISAPITestTool
    {
    public partial class Form1 : Form
    {
    private string FileName;
    private string LastChoseFolder;
    public Form1()
    {
    InitializeComponent();
    }

    private void btnFileChose_Click(object sender, EventArgs e)
    {
    OpenFileDialog fileDialog = new OpenFileDialog();

    DialogResult dr= fileDialog.ShowDialog();
    if (dr == DialogResult.OK)
    {
    string filename = fileDialog.FileName;
    tbFilePath.Text = filename;
    FileName = fileDialog.SafeFileName;
    }
    }

    private void btnToString_Click(object sender, EventArgs e)
    {
    if (File.Exists(tbFilePath.Text.Trim()))
    {
    FileStream fs = new FileStream(tbFilePath.Text.Trim(), FileMode.Open, FileAccess.Read);
    byte[] buffByte = new byte[fs.Length];
    fs.Read(buffByte, 0, (int)fs.Length);
    fs.Close();
    fs = null;
    string str = Convert.ToBase64String(buffByte);
    tbFleString.Text = str;

    }
    else {
    MessageBox.Show("文件不存在!");
    }

    }

    private void btnToFile_Click(object sender, EventArgs e)
    {
    if (!string.IsNullOrWhiteSpace(tbFleString.Text.Trim()))
    {
    FolderBrowserDialog fb = new FolderBrowserDialog();
    fb.SelectedPath = LastChoseFolder;
    DialogResult dr= fb.ShowDialog();
    if (dr == DialogResult.OK)
    {
    LastChoseFolder = fb.SelectedPath;
    string str = tbFleString.Text.Trim();
    byte[] buff = Convert.FromBase64String(str);

    FileStream fs2;
    FileInfo fi = new FileInfo(LastChoseFolder + "\\" + FileName);
    fs2 = fi.OpenWrite();
    fs2.Write(buff, 0, buff.Length);
    fs2.Close();
    MessageBox.Show("转换成功!");
    }
    }

    }

    public byte[] StreamToByte(Stream stream)
    {
    MemoryStream ms = new MemoryStream();
    int b;

    while ((b = stream.ReadByte()) != -1)
    {
    ms.WriteByte((byte)b);

    }
    return ms.ToArray();
    }
    }
    }

  • 相关阅读:
    接水果(fruit)——整体二分+扫描线
    大融合——LCT维护子树信息
    魔卡少女(cardcaptor)——线段树
    而立之年的一些人生感悟
    PHP 输出缓冲控制(Output Control) 学习
    我所了解的cgi
    c语言指针学习
    ubuntu 安装 zend studio
    Zend_Controller_Front 研究
    php autoload 笔记
  • 原文地址:https://www.cnblogs.com/jiebian/p/2795760.html
Copyright © 2020-2023  润新知