Just Copy & Paste the following in a class file. Then Instantiate object of that class and call the methods
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Collections;
/// <summary>
/// Summary description for fileFolderRelated
/// </summary>
///
namespace _________________
{
public partial class FileHandler
{
/// <summary>
/// File can be uploaded to folder through this function
/// </summary>
/// <param name="BrowerControl">FileInput Type control</param>
/// <param name="SaveLocation">The path of the file to upload to folder</param>
/// <returns>It will return the file name that has already uploaded</returns>
public string InsertFileToFolder(System.Web.UI.WebControls.FileUpload BrowserControl,string SaveLocation)
{
string ImageFileName="";
string ImageSaveLocation="";
int status=0;
if (BrowserControl.PostedFile.ContentLength != 0)
{
ImageFileName = System.IO.Path.GetFileName(BrowserControl.PostedFile.FileName.ToString());
ImageSaveLocation=HttpContext.Current.Server.MapPath(SaveLocation) + "\\" + ImageFileName;
if (!System.IO.File.Exists(ImageSaveLocation))
{
BrowserControl.PostedFile.SaveAs(ImageSaveLocation);
}
else
{
System.IO.File.Delete(ImageSaveLocation);
BrowserControl.PostedFile.SaveAs(ImageSaveLocation);
}
}
return ImageFileName;
}
public string InsertFileToFolderWithNewName(System.Web.UI.WebControls.FileUpload BrowserControl, string SaveLocation, string ImageFileName)
{
//string ImageFileName = "";
string ImageSaveLocation = "";
int status = 0;
if (BrowserControl.PostedFile.ContentLength != 0)
{
ImageFileName = ImageFileName + "_" + System.IO.Path.GetFileName(BrowserControl.PostedFile.FileName.ToString());
ImageSaveLocation = HttpContext.Current.Server.MapPath(SaveLocation) + "\\" + ImageFileName;
if (!System.IO.File.Exists(ImageSaveLocation))
{
BrowserControl.PostedFile.SaveAs(ImageSaveLocation);
}
else
{
System.IO.File.Delete(ImageSaveLocation);
BrowserControl.PostedFile.SaveAs(ImageSaveLocation);
}
}
return ImageFileName;
}
/// <summary>
/// creates a folder in server
/// </summary>
/// <param name="folderPath">path of the folder where the new folder will be created</param>
/// <param name="folderName">name of the folder that will be created</param>
public void createFolder(string folderPath, string folderName)
{
if(folderPath == "")
{
DirectoryInfo dInfo = new DirectoryInfo(HttpContext.Current.Server.MapPath(folderName));
dInfo.Create();
}
else
{
DirectoryInfo dInfo = new DirectoryInfo(HttpContext.Current.Server.MapPath(folderPath) + "\\" + folderName);
dInfo.Create();
}
}
/// <summary>
/// get all files in a folder
/// </summary>
/// <param name="folderPath">full path of a folder</param>
/// <returns></returns>
public Array selectAllFilesFromFolder(string folderPath)
{
string[] allFiles = Directory.GetFiles(folderPath);
return allFiles;
}
/// <summary>
/// count files in a folder
/// </summary>
/// <param name="folderPath">full path of a folder</param>
/// <returns></returns>
public int countFilesInFolder(string folderPath)
{
string[] allFiles = Directory.GetFiles(folderPath);
int count = allFiles.Length;
return count;
}
}
}