最近﹐工作不是很忙﹐將一些代碼整理了一些﹐貼出來﹗呵呵....說不定以后就用復著了。
namespace AuthenticationTestClient
{
using System;
using System.IO;
using System.Windows.Forms;
/// <summary>
/// FileLoader 的摘要描述。
/// </summary>
public class FileLoader
{
public FileLoader()
{
//
// TODO: 在此加入建構函式的程式碼
//
}
[FlagsAttribute]
public enum FileStyle {DOC, XLS};
private byte[] _FileArray = new byte [1];
public byte[] FileArray
{
get {return _FileArray;}
}
public static FileLoader LoadFile (FileStyle fFileStyle)
{
FileLoader fFileLoader = new FileLoader ();
OpenFileDialog fOpenFileDialog = new OpenFileDialog ();
fOpenFileDialog.Filter = fFileLoader.CombinFilter (fFileStyle);
if (fOpenFileDialog.ShowDialog () == DialogResult.OK)
{
FileStream fStream = new FileStream (fOpenFileDialog.FileName,
FileMode.Open, FileAccess.Read, FileShare.Read);
fFileLoader._FileArray = new byte[fStream.Length];
fStream.Read (fFileLoader._FileArray,0, (int)fStream.Length);
fStream.Flush ();
fStream.Close ();
}
else
{
return null;
}
return fFileLoader;
}
public static void SaveAs (byte[] fFileArray, FileStyle fFileStyle)
{
FileLoader fFileLoader = new FileLoader ();
SaveFileDialog fFileDialog = new SaveFileDialog ();
fFileDialog.Filter = fFileLoader.CombinFilter (fFileStyle);
if (fFileDialog.ShowDialog() == DialogResult.OK )
{
string fFileName = fFileDialog.FileName ;
FileStream fFile = new FileStream (fFileName,
FileMode.Create, FileAccess .ReadWrite, FileShare.Read);
fFile.Write (fFileArray, 0, fFileArray.Length );
fFile.Flush ();
fFile.Close ();
}
}
/// <summary>
/// 合并enum
/// </summary>
/// <param name="FilterStyle"></param>
/// <returns></returns>
private string CombinFilter (FileStyle FilterStyle)
{
string FilterString = "";
FileStyle[] StyleArray = (FileStyle[])Enum.GetValues (FilterStyle.GetType());
for (int i = 0; i < StyleArray.Length; i++)
{
if (FilterString == "")
FilterString = FilterStr(StyleArray[i]);
else
{
FilterString = FilterString + '|' + FilterStr (StyleArray[i]);
}
}
return FilterString;
}
/// <summary>
/// 取得實際的篩選值
/// </summary>
/// <param name="FilterStyle"></param>
/// <returns></returns>
private string FilterStr (FileStyle FilterStyle)
{
string FilterString = "";
switch (FilterStyle)
{
case FileStyle.DOC:
FilterString = "DOC (*.doc)|*.doc";
break;
case FileStyle.XLS:
FilterString = "XLS (*.xls)|*.xls";
break;
default:
break;
}
return FilterString ;
}
}
}
至于有何作用﹐使用如下方法調用一下你就知道了。有點懶﹐自己看吧。
namespace AuthenticationTestClient
{
using System;
using System.IO;
using System.Windows.Forms;
/// <summary>
/// FileLoader 的摘要描述。
/// </summary>
public class FileLoader
{
public FileLoader()
{
//
// TODO: 在此加入建構函式的程式碼
//
}
[FlagsAttribute]
public enum FileStyle {DOC, XLS};
private byte[] _FileArray = new byte [1];
public byte[] FileArray
{
get {return _FileArray;}
}
public static FileLoader LoadFile (FileStyle fFileStyle)
{
FileLoader fFileLoader = new FileLoader ();
OpenFileDialog fOpenFileDialog = new OpenFileDialog ();
fOpenFileDialog.Filter = fFileLoader.CombinFilter (fFileStyle);
if (fOpenFileDialog.ShowDialog () == DialogResult.OK)
{
FileStream fStream = new FileStream (fOpenFileDialog.FileName,
FileMode.Open, FileAccess.Read, FileShare.Read);
fFileLoader._FileArray = new byte[fStream.Length];
fStream.Read (fFileLoader._FileArray,0, (int)fStream.Length);
fStream.Flush ();
fStream.Close ();
}
else
{
return null;
}
return fFileLoader;
}
public static void SaveAs (byte[] fFileArray, FileStyle fFileStyle)
{
FileLoader fFileLoader = new FileLoader ();
SaveFileDialog fFileDialog = new SaveFileDialog ();
fFileDialog.Filter = fFileLoader.CombinFilter (fFileStyle);
if (fFileDialog.ShowDialog() == DialogResult.OK )
{
string fFileName = fFileDialog.FileName ;
FileStream fFile = new FileStream (fFileName,
FileMode.Create, FileAccess .ReadWrite, FileShare.Read);
fFile.Write (fFileArray, 0, fFileArray.Length );
fFile.Flush ();
fFile.Close ();
}
}
/// <summary>
/// 合并enum
/// </summary>
/// <param name="FilterStyle"></param>
/// <returns></returns>
private string CombinFilter (FileStyle FilterStyle)
{
string FilterString = "";
FileStyle[] StyleArray = (FileStyle[])Enum.GetValues (FilterStyle.GetType());
for (int i = 0; i < StyleArray.Length; i++)
{
if (FilterString == "")
FilterString = FilterStr(StyleArray[i]);
else
{
FilterString = FilterString + '|' + FilterStr (StyleArray[i]);
}
}
return FilterString;
}
/// <summary>
/// 取得實際的篩選值
/// </summary>
/// <param name="FilterStyle"></param>
/// <returns></returns>
private string FilterStr (FileStyle FilterStyle)
{
string FilterString = "";
switch (FilterStyle)
{
case FileStyle.DOC:
FilterString = "DOC (*.doc)|*.doc";
break;
case FileStyle.XLS:
FilterString = "XLS (*.xls)|*.xls";
break;
default:
break;
}
return FilterString ;
}
}
}
至于有何作用﹐使用如下方法調用一下你就知道了。有點懶﹐自己看吧。
FileLoader.SaveAs( FileLoader.LoadFile(FileLoader.FileStyle.DOC).FileArray,FileLoader.FileStyle.DOC);