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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Where is this file? 3 32
How can I do Unit Testing with Session ? 1 23
asp.net figure out error 3 20
Nested forach loop to linq 3 24
The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…

685 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