C# 文件File帮助类

using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;
using System.Web;
using System.Web.UI.WebControls;

namespace https://www.360docs.net/doc/3418222698.html,mon
{
///


/// 沐雪微信平台公共方法:文件帮助类
///

public class FileHelper
{
///
/// 转换为字节数组
///

/// 文件物理路径含文件名
/// 字节数组
public static byte[] GetBinaryFile(string fullName)
{
if (File.Exists(fullName))
{
FileStream Fsm = null;
try
{
Fsm = File.OpenRead(fullName);
return ConvertStreamToByteBuffer(Fsm);
}
catch
{
return new byte[0];
}
finally
{
Fsm.Close();
}
}
else
{
return new byte[0];
}
}

///
/// 流转化为字节数组
///

///
/// 字节数组
public static byte[] ConvertStreamToByteBuffer(System.IO.Stream theStream)
{
int bi;
MemoryStream tempStream = new System.IO.MemoryStream();
try
{
while ((bi = theStream.ReadByte()) != -1)
{
tempStream.WriteByte(((byte)bi));
}
return tempStream.ToArray();
}
catch
{
return new byte[0];
}
finally
{
tempStream.Close();
}
}

///
/// 文件流上传文件
///

/// 字节数组
/// 文件物理路径含文件名
public static bool SaveFile(byte[] binData, string fullName)
{
FileStream fileStream = null;
MemoryStream m = new MemoryStream(binData);
try
{
fileStream = new FileStream(fullName, FileMode.Create);
m.WriteTo(fileStream);
return true;
}
catch
{
return false;
}
finally
{
m.Close();
fileStream.Close();
}
}

///
/// 删除本地单个文件
///

/// 文件相对路径
public static bool DeleteFile(string _filepath)

{
if (string.IsNullOrEmpty(_filepath))
{
return false;
}
string fullpath = Utils.GetMapPath(_filepath);
if (File.Exists(fullpath))
{
File.Delete(fullpath);
return true;
}
return false;
}

///


/// 删除本地上传的文件(及缩略图)
///

///
public static void DeleteUpFile(string _filepath)
{
if (string.IsNullOrEmpty(_filepath))
{
return;
}
string fullpath = Utils.GetMapPath(_filepath); //原图
if (File.Exists(fullpath))
{
File.Delete(fullpath);
}
if (_https://www.360docs.net/doc/3418222698.html,stIndexOf("/") >= 0)
{
string thumbnailpath = _filepath.Substring(0, _https://www.360docs.net/doc/3418222698.html,stIndexOf("/")) + "mall_" + _filepath.Substring(_https://www.360docs.net/doc/3418222698.html,stIndexOf("/") + 1);
string fullTPATH = Utils.GetMapPath(thumbnailpath); //宿略图
if (File.Exists(fullTPATH))
{
File.Delete(fullTPATH);
}
}
}

///
/// 删除内容图片
///

/// 内容
/// 匹配开头字符串
public static void DeleteContentPic(string content, string startstr)
{
if (string.IsNullOrEmpty(content))
{
return;
}
Regex reg = new Regex("IMG[^>]*?src\\s*=\\s*(?:\"(?<1>[^\"]*)\"|'(?<1>[^\']*)')", RegexOptions.IgnoreCase);
MatchCollection m = reg.Matches(content);
foreach (Match math in m)
{
string imgUrl = math.Groups[1].Value;
string fullPath = Utils.GetMapPath(imgUrl);
try
{
if (imgUrl.ToLower().StartsWith(startstr.ToLower()) && File.Exists(fullPath))
{
File.Delete(fullPath);
}
}
catch { }
}
}

///
/// 删除指定文件夹
///

/// 文件相对路径
public static bool DeleteDirectory(string _dirpath)
{
if (string.IsNullOrEmpty(_dirpath))
{
return false;
}
string fullpath = Utils.GetMapPath(_dirpath);
if (Directory.Exists(fullpath))
{
Directory.Delete(fullpath, true);
return true;
}
return false;
}

///
/// 修改指定文件夹名称


///


/// 旧相对路径
/// 新相对路径
/// bool
public static bool MoveDirectory(string old_dirpath, string new_dirpath)
{
if (string.IsNullOrEmpty(old_dirpath))
{
return false;
}
string fulloldpath = Utils.GetMapPath(old_dirpath);
string fullnewpath = Utils.GetMapPath(new_dirpath);
if (Directory.Exists(fulloldpath))
{
Directory.Move(fulloldpath, fullnewpath);
return true;
}
return false;
}

///
/// 返回文件大小KB
///

/// 文件相对路径
/// int
public static int GetFileSize(string _filepath)
{
if (string.IsNullOrEmpty(_filepath))
{
return 0;
}
string fullpath = Utils.GetMapPath(_filepath);
if (File.Exists(fullpath))
{
FileInfo fileInfo = new FileInfo(fullpath);
return ((int)fileInfo.Length) / 1024;
}
return 0;
}

///
/// 返回文件扩展名,不含“.”
///

/// 文件全名称
/// string
public static string GetFileExt(string _filepath)
{
if (string.IsNullOrEmpty(_filepath))
{
return string.Empty;
}
return Path.GetExtension(_filepath).Trim('.');
}

///
/// 返回文件名,不含路径
///

/// 文件相对路径
/// string
public static string GetFileName(string _filepath)
{
return _filepath.Substring(_https://www.360docs.net/doc/3418222698.html,stIndexOf(@"/") + 1);
}

///
/// 文件是否存在
///

/// 文件相对路径
/// bool
public static bool FileExists(string _filepath)
{
string fullpath = Utils.GetMapPath(_filepath);
if (File.Exists(fullpath))
{
return true;
}
return false;
}

///
/// 获得远程字符串
///

public static string GetDomainStr(string key, string uriPath)
{
string result = CacheHelper.Get(key) as string;
if (result == null)
{
https://www.360docs.net/doc/3418222698.html,.WebClient client = new https://www.360docs.net/doc/3418222698.html,.WebClient();
try

{
client.Encoding = System.Text.Encoding.UTF8;
result = client.DownloadString(uriPath);
}
catch
{
result = "暂时无法连接!";
}
CacheHelper.Insert(key, result, 60);
}

return result;
}

}
}

相关文档
最新文档