Esempio n. 1
0
        private void btnScanFolder_Click(object sender, EventArgs exter)
        {
            FolderBrowserDialog FolderDialog = new FolderBrowserDialog();

            FolderDialog.ShowNewFolderButton = false;
            if (FolderDialog.ShowDialog() == DialogResult.OK)
            {
                m_CodePath       = FolderDialog.SelectedPath;
                txtCodePath.Text = m_CodePath;
            }
            else
            {
                return;
            }
            m_CodeFileList.Clear();
            DirectoryInfo dir = new DirectoryInfo(m_CodePath);

            FileInfo[]    finfo;
            List <String> extension = new List <String>();

            extension.Add("*.cs");
            extension.Add("*.c");
            extension.Add("*.h");
            extension.Add("*.cpp");
            foreach (String ext in extension)
            {
                finfo = dir.GetFiles(ext, SearchOption.AllDirectories);//递归搜索
                for (int i = 0; i < finfo.Length; i++)
                {
                    CodeFilePro temp  = new CodeFilePro();
                    String      fpath = finfo[i].FullName;
                    temp.FilePath = fpath;
                    String fname = Path.GetFileName(fpath);
                    temp.FileName = fname;
                    m_CodeFileList.Add(temp);
                }
            }
            UpdateView(false);
        }
Esempio n. 2
0
        public void Read(string path, CodeFilePro cfproperty)
        {
            int          tlines = 0, clines = 0, nlines = 0, blines = 0;
            StreamReader sr = new StreamReader(path, Encoding.Default);
            String       line;
            bool         noteflag = false;//遇见/*noteflag=true 遇到*/noteflag=false

            while ((line = sr.ReadLine()) != null)
            {
                tlines++;
                line = line.Trim();
                if (String.IsNullOrEmpty(line))
                {
                    blines++;
                }
                else
                {
                    if (noteflag == true)
                    {
                        if (line.EndsWith("*/"))
                        {
                            noteflag = false;
                            nlines++;
                        }
                        else
                        {
                            if (line.Contains("*///"))
                            {
                                noteflag = false;
                                nlines++;
                            }
                            else if (line.Contains("*/"))
                            {
                                noteflag = false;
                                clines++;
                            }
                            else
                            {
                                nlines++;
                            }
                        }
                    }
                    else//notefalg == flase
                    {
                        if (line.StartsWith("//"))
                        {
                            nlines++;
                        }
                        else
                        {
                            if (line.StartsWith("/*"))
                            {
                                if (line.EndsWith("*/"))
                                {
                                    nlines++;
                                }
                                else if (line.Contains("*///"))
                                {
                                    nlines++;
                                }
                                else if (line.Contains("*/"))
                                {
                                    clines++;
                                }
                                else
                                {
                                    noteflag = true;
                                    nlines++;
                                }
                            }
                            else
                            {
                                /*特殊情况该如何处理?如何判断“/*”是否有效?
                                 * ...".../*...\"\"\"..."/*
                                 * /*
                                 */
                                if (line.Contains("/*"))     //设置判断/*有效的条件
                                {
                                    if (line.Contains("*/")) // */应该在/*之后
                                    {
                                        clines++;
                                    }
                                    else
                                    {
                                        noteflag = true;
                                        clines++;
                                    }
                                }
                                else
                                {
                                    clines++;
                                }
                            }
                        }
                    }
                }
            }
            cfproperty.TotalLines = tlines;
            cfproperty.CodeLines  = clines;
            cfproperty.NoteLines  = nlines;
            cfproperty.BlankLines = blines;
        }