ToolStripLabel text

flynn

Regular
Joined
Jul 28, 2005
Messages
59
On my form, I have a ToolStrip that contains a ToolStripLabel that has it's 'Text' property set to "C:\".

When I run the following code, the "diDirectory.GetFiles()" method throws an "Illegal characters in path." exception.

Code:
            DirectoryInfo diDirectory = new DirectoryInfo(tsPathLabel.Text);

            FileInfo[] Files = diDirectory.GetFiles(tsFilter.Text);

            this.SuspendLayout();
            foreach (FileInfo File in Files)
            {
                ListViewItem lvi = new ListViewItem(new string[] {File.Name, File.Length.ToString() });
                listView1.Items.Add(lvi);
            }
            this.ResumeLayout();

This is what is displayed when I print the label in the Immediate window:

? tsPathLabel.Text
"\"C:\\\""

? tsPathLabel.Text.ToString()
"\"C:\\\""


The text I was expecting is "C:\". Is there a reason that I'm seeing the text above instead of what I was expecting?

tia
flynn
 
The text of the tool strip label is
"C:\"
as opposed to
C:\

You have the quotes included in the string. The reason for the error is that quotation marks are not valid in folder or file names. The reason for the extra backslashes is that the string is being displayed as a c-style escaped string, so this string
"C:\"
will be displayed with a backslash before each quote or backslash, with a pair of surrounding quotes, like so:
"\"C:\\\""
 
I understand that the quote character is an illegal character in a path name. What I'm not understanding is why the GetPath() call returns such a mangled string ("\"C:\\\"") and not just C:\ (or at least C:\\).

I have attached 2 print screens for reference. When the exception occurs, I execute the GetPath() method in the Immediate window (shown in the print screen). It shows the mangled string. Screen shot 2 shows the value of the tsPathLabel as I entered it (C:\) in the properties window.

So I guess what I don't understand is why the string has all the 'escaping' when all I enter is C:\ .

I can do this:
Code:
tsPathLabel.Text.Replace("\"","");
but is this a valid way to handle the problem?
 

Attachments

The escape sequences are not part of the string. That is how the string is represented in c# code. When you see "\"C:\\\"" in the code, in the immediate window, or in a watch, that is the c# representation of the string "C:\" (quotes included). The backslashes are not actually part of the string.

Your only problem is that the string contains quotes. I see that there are no quotes in there at design time, and I don't see any quotes in the code you posted, but at some point quotation marks are being introduced into the string.
 
Back
Top