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();
}
}
}