locked
Illegal characters in path RRS feed

  • Question

  • I am using a ComboBox that has file paths; @"C:\Users\Name\My Documents\File.txt"

    I have ComboBox1 select the file from the Collects that pull down. When I use the ComboBox in

    FileStream or StreamWriter as StreamWrite sw = new StreamWriter(Combobox1.Text); for file path and name I get - "Illegal characters in path".

    Saturday, September 24, 2011 7:43 PM

Answers

  • But this is not the problem, I assume the problem is that file does not exist, tr this code, it has to work (it does for me):

            private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
            {
                string file = comboBox2.SelectedItem.ToString();
                if (!File.Exists(file))
                {
                    FileStream fs = new FileStream(file, FileMode.CreateNew);
                    fs.Close();
                }
                using (StreamWriter sw = new StreamWriter(file))
                { 
                
                }
            }
    



    Mitja
    • Marked as answer by sojour Saturday, September 24, 2011 8:30 PM
    Saturday, September 24, 2011 7:52 PM
  • If the ComboBox has this value in it:

        @"C:\Users\Name\My Documents\File.txt"

    Then it's probably complaining about the @ symbol and the quotes. The @ symbol is used in code to indicate that the following string's \ characters should be treated literally instead of as escape characters. The goal of @ and the quotes is to tell the program what value to put into a string variable. If you look at the value after it is entered, you won't see those characters in the actual string.

    But here you're getting the string from a control. Whatever the control displays is what will really be put in the string. Probably in this example you need to put this string in the ComboBox:

        C:\Users\Name\My Documents\File.txt


    Rod Stephens, Visual Basic Programming 24-Hour Trainer (http://www.vb-helper.com/24hourvb.htm)
    • Marked as answer by Lie You Monday, October 3, 2011 3:31 AM
    Saturday, September 24, 2011 9:01 PM

All replies

  • You should use ComboBox.SelectedText or ComboBox.SelectedItem property.


    • Edited by Adnan Dedic Saturday, September 24, 2011 7:48 PM
    Saturday, September 24, 2011 7:46 PM
  • Use inside parentheses as argument:

    comboBox1.SelectedItem.ToString();
    



    Mitja
    Saturday, September 24, 2011 7:47 PM
  • But this is not the problem, I assume the problem is that file does not exist, tr this code, it has to work (it does for me):

            private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
            {
                string file = comboBox2.SelectedItem.ToString();
                if (!File.Exists(file))
                {
                    FileStream fs = new FileStream(file, FileMode.CreateNew);
                    fs.Close();
                }
                using (StreamWriter sw = new StreamWriter(file))
                { 
                
                }
            }
    



    Mitja
    • Marked as answer by sojour Saturday, September 24, 2011 8:30 PM
    Saturday, September 24, 2011 7:52 PM
  • I get empty path not legal
    Saturday, September 24, 2011 7:59 PM
  • I get illegal characters in path.
    Saturday, September 24, 2011 7:59 PM
  • When you debug the code: What is inside the file variable after 
    string file = comboBox2.SelectedItem.ToString();

    Just debug the program to find out, what is inside the variables and what is going on exactly.

    With kind regards,

    Konrad

    Saturday, September 24, 2011 8:04 PM
  • Hmm.. then is something wrong with the path...

    You know what you can do, since your file is in MyDocuments, try to get the MyDocuments path by C# code, and add only your file name:

     

     

    //when adding items to comboBox:
    string pathMyDocuments = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
    comboBox2.Items.Add(System.IO.Path.Combine(pathMyDocuments, "File.txt"));

    Do the same for the other items (files).

    Mitja
    Saturday, September 24, 2011 8:06 PM
  • If the ComboBox has this value in it:

        @"C:\Users\Name\My Documents\File.txt"

    Then it's probably complaining about the @ symbol and the quotes. The @ symbol is used in code to indicate that the following string's \ characters should be treated literally instead of as escape characters. The goal of @ and the quotes is to tell the program what value to put into a string variable. If you look at the value after it is entered, you won't see those characters in the actual string.

    But here you're getting the string from a control. Whatever the control displays is what will really be put in the string. Probably in this example you need to put this string in the ComboBox:

        C:\Users\Name\My Documents\File.txt


    Rod Stephens, Visual Basic Programming 24-Hour Trainer (http://www.vb-helper.com/24hourvb.htm)
    • Marked as answer by Lie You Monday, October 3, 2011 3:31 AM
    Saturday, September 24, 2011 9:01 PM
  • I doubt tha "@" symbol can be the reason for this issue, not it this case. Try it by your self.

    If this case there are two possible solutions:

    1.  @"C:\Users\Name\My Documents\File.txt" 

    2.  "C:\\Users\\Name\\My Documents\\File.txt"

    "@" simbol only indicateas that its not need to write double back-slashes for valid path. But both cases are "equal" for the system (recognizing the valid path).


    Mitja
    Saturday, September 24, 2011 9:18 PM
  • The @ and \\ symbols are only for use in your code. This path is not coming from code, it's coming from a control and it has no idea that those symbols mean anything special. It's as if you entered @"C:\Users\Name\My Documents\File.txt" in a TextBox. Or even if you opened Windows Explorer and entered it in the navigation area. The @, ", \\, and other symbols get entered literally and confuse Windows Explorer.

    Try putting just the true path with no special symbols C:\Users\Name\My Documents\File.txt in the ComboBox and see what happens.


    Rod Stephens, Visual Basic Programming 24-Hour Trainer (http://www.vb-helper.com/24hourvb.htm)
    Sunday, September 25, 2011 3:21 AM