Solved

Need textbox.text property to add two (2) backslashes for each directory level that is added to control

Posted on 2008-10-20
5
279 Views
Last Modified: 2013-12-17
1. Being that this is C#, I would like my code to add two (2) backslashes for each directory that is added to the Textbox.text control. Ex: \\server\share\subdir1 or \\server\share\subdir1\subdir2, etc. That would need to take place on line 35 or where code reads "string dirName = textBoxSearchPath.Text = "\\\\corpsrv1\\logs$\\sd4\\regional". I JUST SET THE VALUE right now for testing.
2. When it gets to line 58 or where code reads "private void CreateHeadersAndFillListView", the program hangs. It's crashed my Vista system once (BSOD) and I don't want it to send my laptop into another BSOD. This share I'm trying to connect to and probably had around 200 - 300 log files it must parse through. Any ideas on what I might be leaving out?
3. I'm still new to C# so bare with me please.
Thank you,
Wally
using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using System.IO;
 

namespace ParseLogErrors

{

    public partial class Form1 : Form

    {

        private System.Collections.Specialized.StringCollection folderCol;

        private ImageList ilLarge;

        private ImageList ilSmall;

        //private ListView lvwFilesAndFolders;

        //private Label lblCurrentPath;
 

        public Form1()

        {

            InitializeComponent();
 

            // Init ListView and folder collection

            folderCol = new System.Collections.Specialized.StringCollection();

            CreateHeadersAndFillListView();

            PaintListView(@"\\\\corpsrv1\logs$\sd4\regional");

            folderCol.Add(@"\\\\corpsrv1\logs$\sd4\regional");
 

            //this.lvwFilesAndFolders.ItemActivate += System.EventHandler(this.lvwFilesAndFolders_ItemActivate);

        }
 

        private void buttonParseLogs_Click(object sender, EventArgs e)

        {

            string dirName = textBoxSearchPath.Text = "\\\\corpsrv1\\logs$\\sd4\\regional";

            //DirectoryInfo dir1 = new DirectoryInfo("\\" + dirName);

            MessageBox.Show(dirName);
 

            // Set current working directory & verify it exists

            if (Directory.Exists(dirName))

            try

            {

                Directory.SetCurrentDirectory(dirName);

                MessageBox.Show(dirName);

            }

            catch (UnauthorizedAccessException)

            {

                MessageBox.Show("Not authorized to access " + dirName);

                return;

            }

            catch (FileNotFoundException)

            {

                MessageBox.Show("No such directory: " + dirName);

                return;

            }    

        }
 

        private void CreateHeadersAndFillListView()

        {

            ColumnHeader colHead;
 

            colHead = new ColumnHeader();

            colHead.Text = "Filename";

            this.lvwFilesAndFolders.Columns.Add(colHead);
 

            colHead = new ColumnHeader();

            colHead.Text = "Size";

            this.lvwFilesAndFolders.Columns.Add(colHead);
 

            colHead = new ColumnHeader();

            colHead.Text = "Last Accessed";

            this.lvwFilesAndFolders.Columns.Add(colHead);

        }
 

        private void PaintListView(string path)

        {

            try

            {

                ListViewItem lvi;

                ListViewItem.ListViewSubItem lvsi;
 

                this.labelDisplayLogInfo.Text = path + "Double click to display";

                DirectoryInfo dir = new DirectoryInfo(path);
 

                DirectoryInfo[] dirs = dir.GetDirectories();

                FileInfo[] files = dir.GetFiles();
 

                this.lvwFilesAndFolders.Items.Clear();
 

                this.lvwFilesAndFolders.BeginUpdate();
 

                foreach (System.IO.DirectoryInfo di in dirs)

                {

                    lvi = new ListViewItem();

                    lvi.Text = di.Name;

                    lvi.Tag = di.FullName;
 

                    lvsi = new ListViewItem.ListViewSubItem();

                    lvsi.Text = "sub item";

                    lvi.SubItems.Add(lvsi);
 

                    lvsi = new ListViewItem.ListViewSubItem();

                    lvsi.Text = di.LastAccessTime.ToString();

                    lvi.SubItems.Add(lvsi);

                }
 

                this.lvwFilesAndFolders.EndUpdate();

            }

            catch (System.Exception err)

            {

                MessageBox.Show("Error: " + err.Message);

            }
 

            this.lvwFilesAndFolders.View = View.Details;

        }
 

        private void lvwFilesAndFolders_ItemActive(object sender, System.EventArgs e)

        {

            ListView lvw = (ListView)sender;

            string filename = lvw.SelectedItems[0].Tag.ToString();

            Console.WriteLine(filename);

        }

    }

}

Open in new window

0
Comment
Question by:wally_davis
  • 3
5 Comments
 
LVL 2

Accepted Solution

by:
MasterPage earned 400 total points
Comment Utility
On lines 27 and 28 you've got too many backslashes at the beginning of each path string.  The @ in front of a string tells the compiler to take it literally and not try to parse any escape codes (things that start with backslashes).
With a @ you would say:
@"\\corpsrv1\logs$\sd4\regional"
or without you would say:
"\\\\corpsrv1\\logs$\\sd4\\regional"

If that doesn't  help, please explain a little more about exactly you're having it hang.  Line 58 isn't somewhere you'd see it hang.  Sometimes it can be a little deceiving as to which line it's actually having problems with.  Have you tried stepping through to see what the last line to execute is before it crashes/hangs?
0
 
LVL 3

Assisted Solution

by:trunghieubkit
trunghieubkit earned 100 total points
Comment Utility
Hi,

Replacing
           PaintListView(@"\\\\corpsrv1\logs$\sd4\regional");            folderCol.Add(@"\\\\corpsrv1\logs$\sd4\regional");
By
            PaintListView(@"\\corpsrv1\logs$\sd4\regional");
            folderCol.Add(@"\\corpsrv1\logs$\sd4\regional");


0
 

Author Comment

by:wally_davis
Comment Utility
Thanks guys, I appreciate the feedback. You've solved the mystery to my first problem.
The code is good right up to setting the current Directory, i.e. Directory.SetCurrentDirectory(dirName);"
The second problem I'm have is when I Step Into the code after setting the directory. I click on the "Step Into" button and when it's supposed to hit the line of code that reads "private void CreateHeadersAndFillListView()" (Line 58 in Code example at top of page), it goes into LaLa Land and for the life of me, I can't figure out why.
0
 

Author Comment

by:wally_davis
Comment Utility
i.e. Is there some Class.Method I've not incorporated to make the rest of this code "Run"?
0
 

Author Comment

by:wally_davis
Comment Utility
Figured out my second question. If forgot to write the routine that adds each directory OR file to the ListViewItem. --> this.lvwFilesAndFolders.Items.Add(lvi);
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now