创建文件夹 文件File操作方法
protected void Page_Load(object sender, EventArgs e)
{
string strPath = Server.MapPath("upload");
List<FileInfo> fList = GetAllFilesInDirectory(strPath);
foreach (var item in fList)
{
Response.Write(item.FullName + "<br>");
}
// DirectoryInfo df=new DirectoryInfo()
string userPath = "test";
if (Directory.Exists(Server.MapPath("upload/" + userPath)))
{
Response.Write("true");
DirectoryInfo userFile = new DirectoryInfo(Server.MapPath("upload/" + userPath));
long fileLong = GetDictSize(Server.MapPath("upload"));
//float f = float.MinValue;
//float.TryParse((fileLong * 1000 / (1024 * 1024)).ToString(), out f);
//Response.Write(f.ToString());
float f = (float)fileLong;
Response.Write((f / (1024 * 1024)));
}
else
{
Response.Write("false");
}
}
private long GetDictSize(string path)
{
if (!System.IO.Directory.Exists(path))
return 0;
string[] fs = System.IO.Directory.GetFiles(path, "*.*", System.IO.SearchOption.AllDirectories);
//获取该文件夹中所有的文件名
long ll = 0;
foreach (string f in fs)
{
dynamic fa = System.IO.File.GetAttributes(f);
System.IO.FileInfo fi = new System.IO.FileInfo(f);
ll += fi.Length;
}
return ll;
}
/// <summary>
/// 返回指定目录下的所有文件信息
/// </summary>
/// <param name="strDirectory"></param>
/// <returns></returns>
public List<FileInfo> GetAllFilesInDirectory(string strDirectory)
{
List<FileInfo> listFiles = new List<FileInfo>(); //保存所有的文件信息
DirectoryInfo directory = new DirectoryInfo(strDirectory);
DirectoryInfo[] directoryArray = directory.GetDirectories();
FileInfo[] fileInfoArray = directory.GetFiles();
if (fileInfoArray.Length > 0) listFiles.AddRange(fileInfoArray);
foreach (DirectoryInfo _directoryInfo in directoryArray)
{
DirectoryInfo directoryA = new DirectoryInfo(_directoryInfo.FullName);
DirectoryInfo[] directoryArrayA = directoryA.GetDirectories();
FileInfo[] fileInfoArrayA = directoryA.GetFiles();
if (fileInfoArrayA.Length > 0) listFiles.AddRange(fileInfoArrayA);
GetAllFilesInDirectory(_directoryInfo.FullName);//递归遍历
}
return listFiles;
}
http://www.58os.com