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
291 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

617 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