.net檔案流操作
阿新 • • 發佈:2019-01-06
/// <summary>
/// 刪除單個檔案
/// </summary>
/// <param name="_filepath">檔案相對路徑</param>
public static bool DeleteFile(string _filepath)
{
if (string.IsNullOrEmpty(_filepath))
{
return false;
}
string fullpath = GetMapPath(_filepath);
if (File.Exists(fullpath))
{
File.Delete(fullpath);
return true;
}
return false;
}
/// <summary>
/// 刪除上傳的檔案(及縮圖)
/// </summary>
/// <param name="_filepath"> </param>
public static void DeleteUpFile(string _filepath)
{
if (string.IsNullOrEmpty(_filepath))
{
return;
}
string fullpath = GetMapPath(_filepath); //原圖
if (File.Exists(fullpath))
{
File.Delete(fullpath);
}
if (_filepath.LastIndexOf("/") >= 0)
{
string thumbnailpath = _filepath.Substring(0, _filepath.LastIndexOf("/")) + "mall_" + _filepath.Substring(_filepath.LastIndexOf("/") + 1);
string fullTPATH = GetMapPath(thumbnailpath); //宿略圖
if (File.Exists(fullTPATH))
{
File.Delete(fullTPATH);
}
}
}
/// <summary>
/// 刪除內容圖片
/// </summary>
/// <param name="content">內容</param>
/// <param name="startstr">匹配開頭字串</param>
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 = GetMapPath(imgUrl);
try
{
if (imgUrl.ToLower().StartsWith(startstr.ToLower()) && File.Exists(fullPath))
{
File.Delete(fullPath);
}
}
catch { }
}
}
/// <summary>
/// 刪除指定資料夾
/// </summary>
/// <param name="_dirpath">檔案相對路徑</param>
public static bool DeleteDirectory(string _dirpath)
{
if (string.IsNullOrEmpty(_dirpath))
{
return false;
}
string fullpath = GetMapPath(_dirpath);
if (Directory.Exists(fullpath))
{
Directory.Delete(fullpath, true);
return true;
}
return false;
}
/// <summary>
/// 修改指定資料夾名稱
/// </summary>
/// <param name="old_dirpath">舊相對路徑</param>
/// <param name="new_dirpath">新相對路徑</param>
/// <returns>bool</returns>
public static bool MoveDirectory(string old_dirpath, string new_dirpath)
{
if (string.IsNullOrEmpty(old_dirpath))
{
return false;
}
string fulloldpath = GetMapPath(old_dirpath);
string fullnewpath = GetMapPath(new_dirpath);
if (Directory.Exists(fulloldpath))
{
Directory.Move(fulloldpath, fullnewpath);
return true;
}
return false;
}
/// <summary>
/// 返回檔案大小KB
/// </summary>
/// <param name="_filepath">檔案相對路徑</param>
/// <returns>int</returns>
public static int GetFileSize(string _filepath)
{
if (string.IsNullOrEmpty(_filepath))
{
return 0;
}
string fullpath = GetMapPath(_filepath);
if (File.Exists(fullpath))
{
FileInfo fileInfo = new FileInfo(fullpath);
return ((int)fileInfo.Length) / 1024;
}
return 0;
}
/// <summary>
/// 返回副檔名,不含“.”
/// </summary>
/// <param name="_filepath">檔案全名稱</param>
/// <returns>string</returns>
public static string GetFileExt(string _filepath)
{
if (string.IsNullOrEmpty(_filepath))
{
return "";
}
if (_filepath.LastIndexOf(".") > 0)
{
return _filepath.Substring(_filepath.LastIndexOf(".") + 1); //副檔名,不含“.”
}
return "";
}
/// <summary>
/// 返回檔名,不含路徑
/// </summary>
/// <param name="_filepath">檔案相對路徑</param>
/// <returns>string</returns>
public static string GetFileName(string _filepath)
{
return _filepath.Substring(_filepath.LastIndexOf(@"/") + 1);
}
/// <summary>
/// 檔案是否存在
/// </summary>
/// <param name="_filepath">檔案相對路徑</param>
/// <returns>bool</returns>
public static bool FileExists(string _filepath)
{
string fullpath = GetMapPath(_filepath);
if (File.Exists(fullpath))
{
return true;
}
return false;
}
/// <summary>
/// 獲得遠端字串
/// </summary>
public static string GetDomainStr(string key, string uriPath)
{
string result = CacheHelper.Get(key) as string;
if (result == null)
{
System.Net.WebClient client = new System.Net.WebClient();
try
{
client.Encoding = System.Text.Encoding.UTF8;
result = client.DownloadString(uriPath);
}
catch
{
result = "暫時無法連線!";
}
CacheHelper.Insert(key, result, 60);
}
return result;
}
如有問題,請加我QQ:631931078或352167311