locked
streamwriter Illegal characters in path RRS feed

  • Question

  •             StreamWriter  test;
                test= new StreamWriter("C:\test.txt");
                test.WriteLine("asfsadfsfs")

     

    whats the problem with this code

    error message:Illegal characters in path


    Wednesday, June 29, 2011 8:59 PM

Answers

  • You have a tab character (\t) in your path.  That is why it is not legal.  Re-write your code like this:

    using (StreamWriter test = new StreamWriter(@"c:\test.txt"))
    {
      test.WriteLine("asfsadfsfs");
    }
    

    The using block makes sure that the stream writer is disposed.  The @ symbol in front of the string treats it as a literal string so that the \t is not interpreted as a tab character.

    Chris

    • Marked as answer by Lie You Friday, July 8, 2011 2:16 AM
    Wednesday, June 29, 2011 9:02 PM
  • StreamWriter inherits from TextWriter which implements the IDisposable interface.  Therefore, you should call .Dispose() on the StreamWriter instance when you are done with it.  Putting the declaration in a using block is easier as it will automatically call Dispose when it exits the block.

    The code from your tutorial is not wrong, but it does not tell the whole story!

    • Marked as answer by Lie You Friday, July 8, 2011 2:16 AM
    Thursday, June 30, 2011 4:13 PM
  •             StreamWriter  test;
                test= new StreamWriter("C:\test.txt");
                test.WriteLine("asfsadfsfs")

     

    whats the problem with this code

    error message:Illegal characters in path



    Hello Jim_21,

     

    try 

     StreamWriter test= new StreamWriter(@"C:\test.txt");
     test.WriteLine("asfsadfsfs");
    
    

          test.Close();


     Bye


    Carmelo La Monica  http://community.visual-basic.it/carmelolamonica/



    • Marked as answer by Lie You Friday, July 8, 2011 2:16 AM
    Wednesday, June 29, 2011 9:03 PM

All replies

  • You have a tab character (\t) in your path.  That is why it is not legal.  Re-write your code like this:

    using (StreamWriter test = new StreamWriter(@"c:\test.txt"))
    {
      test.WriteLine("asfsadfsfs");
    }
    

    The using block makes sure that the stream writer is disposed.  The @ symbol in front of the string treats it as a literal string so that the \t is not interpreted as a tab character.

    Chris

    • Marked as answer by Lie You Friday, July 8, 2011 2:16 AM
    Wednesday, June 29, 2011 9:02 PM
  •             StreamWriter  test;
                test= new StreamWriter("C:\test.txt");
                test.WriteLine("asfsadfsfs")

     

    whats the problem with this code

    error message:Illegal characters in path



    Hello Jim_21,

     

    try 

     StreamWriter test= new StreamWriter(@"C:\test.txt");
     test.WriteLine("asfsadfsfs");
    
    

          test.Close();


     Bye


    Carmelo La Monica  http://community.visual-basic.it/carmelolamonica/



    • Marked as answer by Lie You Friday, July 8, 2011 2:16 AM
    Wednesday, June 29, 2011 9:03 PM
  • the tutorial im reading tells to write like

    Streamwrite writer;

    writer = new StreamWriter ("text.txt");

    and is a univercity teacher who wrote it

    why do you think he wrote it like this

    edit i have write it as carmelo told me and now it tells me access denied

    how to skip this problem

    • Edited by Jim_21 Wednesday, June 29, 2011 9:10 PM
    Wednesday, June 29, 2011 9:06 PM
  • How about just do this:

    File.WriteAllText(@"C:\test.txt", "asfsadfsfs");
    



    John Grove, MCC - Senior Software Engineer
    Wednesday, June 29, 2011 9:10 PM
  • How about just do this:

     

    File.WriteAllText(@"C:\test.txt", "asfsadfsfs");
    


     


    John Grove, MCC - Senior Software Engineer
    hey man i have tried your code but also access denied how to make it to write
    Wednesday, June 29, 2011 9:13 PM
  • According to MSDN, UnauthorizedAccessException can also be caused by:

        path specified a file that is read-only.

        -or-

        This operation is not supported on the current platform.

        -or-

        path specified a directory.

    Is it possible one of these conditions is the cause of your problem?

    John Grove, MCC - Senior Software Engineer
    Wednesday, June 29, 2011 9:16 PM
  • no file with the same name is there

    im running windows 7

    maybe is the uac protection?

    how can i skip it

    is one solution to find the compiled exe and run it as administrator?

    Wednesday, June 29, 2011 9:20 PM
  • you can avoid that problem by not attempting to write to the root directory (C:\).  Place the file someplace else.
    Wednesday, June 29, 2011 9:29 PM
  • I agree with Chris, write it to some place else like My Documents

    String myDocuments = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
    File.WriteAllText(myDocuments, "asfsadfsfs");



    John Grove, MCC - Senior Software Engineer
    Wednesday, June 29, 2011 9:34 PM
  • no file with the same name is there

    im running windows 7

    maybe is the uac protection?

    how can i skip it

    is one solution to find the compiled exe and run it as administrator?


    Hello Jim_21,

    Later on if you use Windows XP, on the 'C drive can not write unless you run your application with administrator rights, I agree with Chris to write any document or folder in the folder of' user.

    Hello

     


    Carmelo La Monica  http://community.visual-basic.it/carmelolamonica/

    Wednesday, June 29, 2011 9:42 PM
  • StreamWriter inherits from TextWriter which implements the IDisposable interface.  Therefore, you should call .Dispose() on the StreamWriter instance when you are done with it.  Putting the declaration in a using block is easier as it will automatically call Dispose when it exits the block.

    The code from your tutorial is not wrong, but it does not tell the whole story!

    • Marked as answer by Lie You Friday, July 8, 2011 2:16 AM
    Thursday, June 30, 2011 4:13 PM