Tuesday, April 28, 2009

File And Folder Related Operation In C#.

 public ReplaceFile(string NewValue){

//  var path = "C:/Documents and Settings/Administrator/My Documents/Visual Studio 2008/Projects/RajanTest1/RajanTest1/Content/ReplaceTest.css";

//  var path = ConfigurationManager.AppSettings["HostName"]+"/Content/ReplaceTest.css";

            var path = AppDomain.CurrentDomain.BaseDirectory.ToString() + "Content/ReplaceTest.css";

            try{

                FileStream fs = new FileStream(path, FileMode.Create);

                StreamWriter sw = new StreamWriter(fs);

                sw.WriteLine(NewValue);

                sw.Write(NewValue);

                sw.Close();

                fs.Close();

                //File.Move(path,"C:\\Documents and Settings\\Administrator\\My Documents\\Visual Studio 2008\\Projects\\RajanTest1\\RajanTest1\\Content\\edit_area\\search_replace.js");

            }

            catch (Exception ee){

                string temp = ee.Message;

            }

 

}

----------------------------------------------------------------------------------------------------------------------------------------------

     

      static public string[] GetAllFileNames(string baseDir)

        {

            //

            // Store results in the file results list.

            //

            List<string> fileResults = new List<string>();

 

            //

            // Store a stack of our directories.

            //

            Stack<string> directoryStack = new Stack<string>();

            directoryStack.Push(baseDir);

 

            //

            // While there are directories to process

            //

            while (directoryStack.Count > 0)

            {

                string currentDir = directoryStack.Pop();

 

                //

// Add all files at this directory.

                //

                foreach (string fileName in Directory.GetFiles(currentDir, "*.*"))

                {

                    fileResults.Add(fileName);

                }

 

                //

                // Add all directories at this directory.

                //

                foreach (string directoryName in Directory.GetDirectories(currentDir))

                {

                    directoryStack.Push(directoryName);

                }

            }

            return fileResults.ToArray();

        }

           

----------------------------------------------------------------------------------------------------------------------------------------------

 

      public string FunctionNAme()

        {

            //Can Get List Of Directory And Also Files

            List<string> fname = new List<string>();

            // DirectoryInfo di = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory.ToString() + "Views");

                DirectoryInfo di = new DirectoryInfo("c:\\MyFolder");

            DirectoryInfo[] dirInfo = di.GetDirectories();

            foreach (DirectoryInfo dir in dirInfo)

            {

 

                DirectoryInfo di1 = new DirectoryInfo(dir.FullName);

                FileInfo[] rgFiles = di1.GetFiles("*.*");//Get All Files Name In Folder

                fname.Add(dir.Name); //Add List Of Folder Name

            }

 

 

            //this will Get path of All Files in specified Path/Folder and also Sub-Folder

            string[] ara = GetAllFileNames("c:\\MyFolder");

 

 // this is to get/read  Content  of File

//var path = AppDomain.CurrentDomain.BaseDirectory.ToString() + "Views/Home/About.aspx";

                  var path="c:\\Myfolder\\Myfile.cs"

            try

            {

                string fileout = string.Empty;

                FileStream fs = new FileStream(path, FileMode.Open);

                StreamReader sr = new StreamReader(fs);

                fileout = sr.ReadToEnd();

                sr.Close();

                fs.Close();

                        return fileout;

            }

            catch (Exception ee)

            {

                string temp = ee.Message;

            }

 

        }

 

----------------------------------------------------------------------------------------------------------------------------------------------

 

//copy files and folders Recursively from sorce to destination

             private void CopyFilesRecursively(DirectoryInfo source, DirectoryInfo target)

    {

        foreach (DirectoryInfo dir in source.GetDirectories())

            CopyFilesRecursively(dir, target.CreateSubdirectory(dir.Name));

        foreach (FileInfo file in source.GetFiles())

            file.CopyTo(Path.Combine(target.FullName, file.Name)).IsReadOnly = false;

    }

 

----------------------------------------------------------------------------------------------------------------------------------------------

 

//Method to delete all files in the folder and subfolders

    private void ClearFolder(DirectoryInfo directoryInfo)

    {

        foreach (FileInfo file in directoryInfo.GetFiles())

        {

            file.IsReadOnly = false;

            file.Delete();

        }

        foreach (DirectoryInfo subfolder in directoryInfo.GetDirectories())

             ClearFolder(subfolder);

      

    }

----------------------------------------------------------------------------------------------------------------------------------------------

static public string[] GetAllDirNames(string baseDir)

        {    //

            // Store results in the directory results list.

            //

            List<string> fileResults = new List<string>(); 

            //

            // Store a stack of our directories.

            //

            Stack<string> directoryStack = new Stack<string>();

            directoryStack.Push(baseDir); 

            //

            // While there are directories to process

            //

            while (directoryStack.Count > 0)

            {

                string currentDir = directoryStack.Pop(); 

                //

                // Add all directories at this directory.

                //

                foreach (string directoryName in      Directory.GetDirectories(currentDir))

                {

                    directoryStack.Push(directoryName);

                    fileResults.Add(directoryName);

                }

            }

            return fileResults.ToArray();

        }

No comments: