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
286 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

739 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