Advertisement

07.02.2008 at 06:34PM PDT, ID: 23535622
[x]
Attachment Details

Calculating lines per file in folders and subfolders

Asked by John500 in C# Programming Language

Tags: Visual Studio .Net 2005 - C#

Greetings:

I need a program that will count the number of lines within a code file (cs).  The routine needs to run against a C# project(s).  Thus, I need it to be able to search all sub folders of a given directory.

So far I have a Program.cs file and a GetFileStats.cs file.  I'm guessing the Program.cs file will call the necessary folder and file routines but I'm not sure how to do this recursively.

Please see the code below.  I primarily need help with how to traverse sub folders.

Thanks  

 

Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
Program.cs
 
using System;
using System.Collections;
using System.Collections.Generic;
using GetFileStats;
 
 
namespace GetProjectStats
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            string FilePath = string.Empty;
            int NumberOfLines;
            FileStats FileStat1 = new FileStats();
 
            foreach (string s in args)
            {
                FilePath = s;  // get one arg for now
            }
             NumberOfLines = FileStat1.ProcessFile(FilePath);
            return;
 
        }
    }
}
 
GetFileStats.cs
 
using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Text;
using System.Threading;
using System.Runtime.InteropServices;
using System.Diagnostics;
 
 
 
namespace GetFileStats
{
 
    public class FileStats
    {
 
        public int ProcessFile(string FilePath)
        {
            string line;
            int linecount = 0;
            try 
	{
		//Pass the file path and file name to the StreamReader constructor		StreamReader sr = new StreamReader(FilePath);
 
		//Read the first line of text
		line = sr.ReadLine();
 
		//Continue to read until you reach end of file
		while (line != null) 
		{
			linecount++;
                                                line = sr.ReadLine();
 
		}
 
		//close the file
		sr.Close();
		Console.ReadLine();
	}
	catch(Exception e)
	{
		Console.WriteLine("Exception: " + e.Message);
	}
                return linecount;
        }
 
        public int ProcessFolder(string FolderPath)
        {
            // For each folder call ProcessFile()
            // ????? 		
            return 0;
        }
 
    }
}
[+][-]07.02.2008 at 08:56PM PDT, ID: 21922583

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07.02.2008 at 08:59PM PDT, ID: 21922593

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07.02.2008 at 09:29PM PDT, ID: 21922688

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07.02.2008 at 10:25PM PDT, ID: 21922840

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07.03.2008 at 12:03AM PDT, ID: 21923278

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: C# Programming Language
Tags: Visual Studio .Net 2005 - C#
Sign Up Now!
Solution Provided By: margajet24
Participating Experts: 2
Solution Grade: A
 
 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628