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
280 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
ID: 22763115
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
ID: 22763754
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
ID: 22767448
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
ID: 22767502
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
ID: 22769054
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

776 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