C#File工具類(持續(xù)更新中......)

這是一份簡單的文件工具類,C#專用。剛接觸C#一個(gè)月,剛好業(yè)務(wù)需求,需要用到,于是自己就寫了一份出來,后續(xù)還會繼續(xù)更新......
‘’‘
using System;
using System.IO;
using System.Web;
using System.Web.Mvc;

namespace ZoneTop.Util
{
/// <summary>
/// 常用公共類
/// </summary>
public class FileHelper
{
#region 文件管理
/// <summary>
/// 文件上傳,創(chuàng)建臨時(shí)物理路徑
/// </summary>
/// <param name="files">文件</param>
/// <param name="strPathPhysicalPart">物理路徑(不全)</param>
/// <param name="strPathNetPart">網(wǎng)絡(luò)路徑(不全)</param>
/// <returns>返回null創(chuàng)建失敗,返回string類型創(chuàng)建成功</returns>
public static string CreateTemp(HttpFileCollectionBase files, string strPathPhysicalPart, string strPathNetPart)
{
//創(chuàng)建虛擬路徑的文件目錄,以時(shí)間為單位創(chuàng)建文件名
DateTime dtFileDate = DateTime.Now;
string strFileDate = dtFileDate.ToString("yyyyMMdd");
strPathPhysicalPart = strPathPhysicalPart + "\" + strFileDate;

        //文件數(shù)量大于或等于1個(gè)
        if (files.Count > 0)
        {
            //獲取第一個(gè)文件
            var file = files[0];

            //判斷第一個(gè)文件是否有內(nèi)容
            if (file.ContentLength > 0)
            {
                //判斷傳入的物理路徑是否存在,不存在創(chuàng)建
                if (!Directory.Exists(strPathPhysicalPart))
                {
                    Directory.CreateDirectory(strPathPhysicalPart);
                }

                //隨機(jī)創(chuàng)建文件名
                string strFileName = System.Guid.NewGuid().ToString() + file.FileName.Substring(file.FileName.LastIndexOf("."));
                //獲取文件全路徑
                string strPathPhysical = strPathPhysicalPart + "\\" + strFileName;
                string strPathNet = strPathNetPart + "/" + strFileDate + "/" + strFileName;
                //寫入文件
                file.SaveAs(strPathPhysical);

                return strPathNet;
            }
            else
            {
                return null;
            }
        }
        else
        {
            return null;
        }
    }

    /// <summary>
    /// 刪除臨時(shí)文件夾
    /// </summary>
    /// <param name="strTemp"></param>
    /// <returns>返回布爾類型</returns>
    public static bool DeleteTemp(string strTemp)
    {
        DirectoryInfo directoryInfo = new DirectoryInfo(strTemp);

        if (directoryInfo.Exists)
        {
            directoryInfo.Delete();
            return true;
        }
        else
        {
            return false;
        }
    }

    /// <summary>
    /// 刪除臨時(shí)文件夾下(有包含文件)
    /// </summary>
    /// <param name="strTemp"></param>
    /// <returns>返回布爾類型</returns>
    public static bool DeleteTemp(string strTemp, bool isFile)
    {
        DirectoryInfo directoryInfo = new DirectoryInfo(strTemp);

        if (directoryInfo.Exists)
        {
            directoryInfo.Delete(isFile);
            return true;
        }
        else
        {
            return false;
        }
    }

    public void SaveFile()
    {
        }
    #endregion
}

}

’‘’

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • pyspark.sql模塊 模塊上下文 Spark SQL和DataFrames的重要類: pyspark.sql...
    mpro閱讀 9,929評論 0 13
  • Lua 5.1 參考手冊 by Roberto Ierusalimschy, Luiz Henrique de F...
    蘇黎九歌閱讀 14,259評論 0 38
  • 一. Java基礎(chǔ)部分.................................................
    wy_sure閱讀 4,031評論 0 11
  • 我認(rèn)識一個(gè)老人,退休后和兒子在一起生活在大城市,拿著還不錯(cuò)的退休金,可以過上很不錯(cuò)的生活。她有兩個(gè)兒子,孩...
    曉蕓will閱讀 583評論 0 0
  • 最近在武志紅老師的《巨嬰國》,讀過好幾本他的書,這一本的確如他所說,不太一樣。大到中國的集體主義,小到嬰兒大小便的...
    霹靂貝貝Suri閱讀 764評論 0 3

友情鏈接更多精彩內(nèi)容