文件相关操作
2016-03-21 10:49
218 查看
using System;
using System.Collections;
using System.Text;
using System.Xml;
using System.IO;
using System.Windows.Forms;
namespace BrainSoft.Common
{
/// <summary>
///
描
述:文件相关操作
/// 参数说明:
///
说 明:
/// 创 建 人:thanktheworld
///
日 期:now
/// 历史修改记录:
///
</summary>
public class FileUtil
{
#region 返回文件二进制信息
///
<summary>
/// 返回文件二进制信息
///
</summary>
public static byte[]
GetFileBytesInfo(string filePath)
{
FileInfo
myFile = new FileInfo(filePath);
FileStream
myFs = myFile.OpenRead();
byte[] bytes
= new byte[myFs.Length];
myFs.Read(bytes,
0, Convert.ToInt32(myFs.Length));
myFs.Close();
myFs = null;
myFile = null;
return
bytes;
}
#endregion
#region 保存二进制信息成文件
///
<summary>
/// 保存二进制信息成文件
///
</summary>
public static bool
SaveBytesToFile(byte[] _file, string filePath)
{
FileStream fs
= new FileStream(filePath, FileMode.CreateNew);
BinaryWriter
bw = new BinaryWriter(fs);
bw.Write(_file,
0, _file.Length);
bw.Close();
fs.Close();
return
true;
}
#endregion
#region 根据路径创建文件夹
///
<summary>
/// 根据路径创建文件夹
///
</summary>
/// <param
name="filePath">文件路径</param>
public static bool
CreateFolder(string filePath)
{
string[]
folders=filePath.Split('\\');
string
folderPath=folders[0];
for (int
i=1;i<folders.Length-1;i++)
{
folderPath=folderPath+@""+folders[i];
if
(Directory.Exists (folderPath)==false)
{
Directory.CreateDirectory(folderPath);
}
}
return
true;
}
#endregion
#region 获取临时文件
///
<summary>
/// 获取临时文件
///
</summary>
///
<returns></returns>
public static string
GetTempFileName()
{
return
GetTempFileName("");
}
///
<summary>
/// 获取临时文件
///
</summary>
/// <param
name="Relative"></param>
///
<returns></returns>
public static string
GetTempFileName(string Relative)
{
string
path = Global.TempPath;
if
(path.EndsWith(@"")==false)
{
path
= path +@"\"+ Relative
;
}
else
{
path
= path + Relative ;
}
if
(!System.IO.Directory.Exists(path))
{
System.IO.Directory.CreateDirectory(path);
}
path +=
System.Guid.NewGuid().ToString();
return
path;
}
#endregion
#region 服务器路径转换为本地路径
///
<summary>
/// 服务器路径转换为本地路径
///
</summary>
/// <param
name="serverPath">服务器路径为
\表名\字锻名\主键.扩展名</param>
///
<returns></returns>
public static string
ConvertServerToLocal(string serverPath)
{
if
(serverPath.IndexOf(":")>-1)
{
return
serverPath;
}
else
{
return
Global.TempPath + serverPath;
}
}
///
<summary>
/// 服务器路径转换为本地路径
///
</summary>
/// <param
name="serverPath">服务器路径为
\表名\字锻名\主键.扩展名</param>
///
<returns></returns>
public static string
ConvertServerToLocalTemp(string serverPath)
{
string[]
folders=serverPath.Split('\\');
string
folderPath=folders[0];
for (int
i=1;i<folders.Length-1;i++)
{
folderPath=folderPath+@""+folders[i];
}
return
Global.TempPath + folderPath + @""+
System.Guid.NewGuid().ToString() + "." +
GetExtName(serverPath);
}
#endregion
#region 本地路径转换为服务器路径
///
<summary>
/// 文件路径转换
///
</summary>
/// <param
name="PK">主键</param>
/// <param
name="serverPath">服务器路径</param>
/// <param
name="filePath">本地路径</param>
///
<returns>\表名\字锻名\主键.扩展名</returns>
public static string
ConvertLocalToServer(string PK,string serverPath,string
filePath)
{
return
serverPath + PK + "." + GetExtName(filePath);
}
#endregion
#region 获取文件扩展名
///
<summary>
/// 获取文件扩展名
///
</summary>
/// <param
name="filePath">文件路径</param>
///
<returns></returns>
public static string
GetExtName(string filePath)
{
string[] arr
= filePath.Split('.');
return
arr[arr.Length - 1];
}
#endregion
}
}
using System.Collections;
using System.Text;
using System.Xml;
using System.IO;
using System.Windows.Forms;
namespace BrainSoft.Common
{
/// <summary>
///
描
述:文件相关操作
/// 参数说明:
///
说 明:
/// 创 建 人:thanktheworld
///
日 期:now
/// 历史修改记录:
///
</summary>
public class FileUtil
{
#region 返回文件二进制信息
///
<summary>
/// 返回文件二进制信息
///
</summary>
public static byte[]
GetFileBytesInfo(string filePath)
{
FileInfo
myFile = new FileInfo(filePath);
FileStream
myFs = myFile.OpenRead();
byte[] bytes
= new byte[myFs.Length];
myFs.Read(bytes,
0, Convert.ToInt32(myFs.Length));
myFs.Close();
myFs = null;
myFile = null;
return
bytes;
}
#endregion
#region 保存二进制信息成文件
///
<summary>
/// 保存二进制信息成文件
///
</summary>
public static bool
SaveBytesToFile(byte[] _file, string filePath)
{
FileStream fs
= new FileStream(filePath, FileMode.CreateNew);
BinaryWriter
bw = new BinaryWriter(fs);
bw.Write(_file,
0, _file.Length);
bw.Close();
fs.Close();
return
true;
}
#endregion
#region 根据路径创建文件夹
///
<summary>
/// 根据路径创建文件夹
///
</summary>
/// <param
name="filePath">文件路径</param>
public static bool
CreateFolder(string filePath)
{
string[]
folders=filePath.Split('\\');
string
folderPath=folders[0];
for (int
i=1;i<folders.Length-1;i++)
{
folderPath=folderPath+@""+folders[i];
if
(Directory.Exists (folderPath)==false)
{
Directory.CreateDirectory(folderPath);
}
}
return
true;
}
#endregion
#region 获取临时文件
///
<summary>
/// 获取临时文件
///
</summary>
///
<returns></returns>
public static string
GetTempFileName()
{
return
GetTempFileName("");
}
///
<summary>
/// 获取临时文件
///
</summary>
/// <param
name="Relative"></param>
///
<returns></returns>
public static string
GetTempFileName(string Relative)
{
string
path = Global.TempPath;
if
(path.EndsWith(@"")==false)
{
path
= path +@"\"+ Relative
;
}
else
{
path
= path + Relative ;
}
if
(!System.IO.Directory.Exists(path))
{
System.IO.Directory.CreateDirectory(path);
}
path +=
System.Guid.NewGuid().ToString();
return
path;
}
#endregion
#region 服务器路径转换为本地路径
///
<summary>
/// 服务器路径转换为本地路径
///
</summary>
/// <param
name="serverPath">服务器路径为
\表名\字锻名\主键.扩展名</param>
///
<returns></returns>
public static string
ConvertServerToLocal(string serverPath)
{
if
(serverPath.IndexOf(":")>-1)
{
return
serverPath;
}
else
{
return
Global.TempPath + serverPath;
}
}
///
<summary>
/// 服务器路径转换为本地路径
///
</summary>
/// <param
name="serverPath">服务器路径为
\表名\字锻名\主键.扩展名</param>
///
<returns></returns>
public static string
ConvertServerToLocalTemp(string serverPath)
{
string[]
folders=serverPath.Split('\\');
string
folderPath=folders[0];
for (int
i=1;i<folders.Length-1;i++)
{
folderPath=folderPath+@""+folders[i];
}
return
Global.TempPath + folderPath + @""+
System.Guid.NewGuid().ToString() + "." +
GetExtName(serverPath);
}
#endregion
#region 本地路径转换为服务器路径
///
<summary>
/// 文件路径转换
///
</summary>
/// <param
name="PK">主键</param>
/// <param
name="serverPath">服务器路径</param>
/// <param
name="filePath">本地路径</param>
///
<returns>\表名\字锻名\主键.扩展名</returns>
public static string
ConvertLocalToServer(string PK,string serverPath,string
filePath)
{
return
serverPath + PK + "." + GetExtName(filePath);
}
#endregion
#region 获取文件扩展名
///
<summary>
/// 获取文件扩展名
///
</summary>
/// <param
name="filePath">文件路径</param>
///
<returns></returns>
public static string
GetExtName(string filePath)
{
string[] arr
= filePath.Split('.');
return
arr[arr.Length - 1];
}
#endregion
}
}
相关文章推荐
- 如何正确配置 Nginx+PHP
- 数据库操作代码
- 64. Minimum Path Sum
- 2012年01月26日
- 好书
- 关于EXCEL表格导入\导出数据…
- 常用asp.net代码
- 想象力的意义
- 关于Ref和Out的区别,附上小例子
- 用DataRow的Select()方法获取DataT…
- C#语言之“实例化的正确顺序”
- 项目复习
- 两分钟彻底让你明白Android中onInterceptTouchEvent与onTouchEvent(图文)!
- Asp.net面试题复习
- C#数组
- Codeforces Round #345 (Div. 1) D. Zip-line
- 服务器负载突然飙高事件
- 归并排序
- toolbar左边留有空隙
- (已解决)内容模型定义的自定义字段在{dede:arclist}中调用不显示结果