Sunday, 30 November 2014

C# File System

//Count lines in file
 public static int CountLinesInFile(string f)
        {
            int count = 0;
            using (StreamReader r = new StreamReader(f))
            {
                string line;
                while ((line = r.ReadLine()) != null)
                {
                    count++;
                }
            }
            return count;
        }

//Create Folder
  public static string createDir(string s)
        {
            try
            {
                // Determine whether the directory exists. 
                if (Directory.Exists(s))
                {
                    return ("That path:" + s + " exists already.");
 
                }
 
                // Try to create the directory.
                DirectoryInfo di = Directory.CreateDirectory(s);
                return ("The directory :" + s + "was created successfully at " + Directory.GetCreationTime(s));
 
 
            }
            catch (Exception e)
            {
                return ("The process failed: " + e.ToString() + " when create:" + s);
            }
            finally { }
        }

No comments:

Post a Comment