Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Opening a file

Opening a file

Scheduled Pinned Locked Moved C#
8 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Darrall
    wrote on last edited by
    #1

    Can anyone tell me why when writing code to open a file you use a forward slash in the path name instead of a back slash as in the following:

    private void button1_Click(object sender, EventArgs e)
    {
    string file_name = "C:/test1.txt";

            System.IO.StreamReader objReader;
            objReader = new System.IO.StreamReader(file\_name);
    
            textBox1.Text = objReader.ReadToEnd();
    
            objReader.Close();
        }
    
    L D J A 4 Replies Last reply
    0
    • D Darrall

      Can anyone tell me why when writing code to open a file you use a forward slash in the path name instead of a back slash as in the following:

      private void button1_Click(object sender, EventArgs e)
      {
      string file_name = "C:/test1.txt";

              System.IO.StreamReader objReader;
              objReader = new System.IO.StreamReader(file\_name);
      
              textBox1.Text = objReader.ReadToEnd();
      
              objReader.Close();
          }
      
      L Offline
      L Offline
      loyal ginger
      wrote on last edited by
      #2

      Because it's easy to type! In this case the slash ("/") and backslash ("\") are both understood as the delimiter for path, so either one should work. However, backslash is used to escape in string literals, so to use backslash, you should escape it, like so: \\. You can see that is more typing than using just slash. If you have a lot of backslashes in your string literal, you can put @ in front of it so you don't need to escape them. For example,

      string file_name = @"C:\test1.txt";

      Still, there is the character @ you have to type. It's still more typing than simply using the forward slash.

      D 1 Reply Last reply
      0
      • D Darrall

        Can anyone tell me why when writing code to open a file you use a forward slash in the path name instead of a back slash as in the following:

        private void button1_Click(object sender, EventArgs e)
        {
        string file_name = "C:/test1.txt";

                System.IO.StreamReader objReader;
                objReader = new System.IO.StreamReader(file\_name);
        
                textBox1.Text = objReader.ReadToEnd();
        
                objReader.Close();
            }
        
        J Offline
        J Offline
        John Whitmire
        wrote on last edited by
        #3

        My guess is that it's simply a cheat to avoid having to type the requisite double backslash (special character):

        string file_name = "C:\\test1.txt";

        ...Byte conservation; it's one byte shorter in the source file. ;P

        D 1 Reply Last reply
        0
        • D Darrall

          Can anyone tell me why when writing code to open a file you use a forward slash in the path name instead of a back slash as in the following:

          private void button1_Click(object sender, EventArgs e)
          {
          string file_name = "C:/test1.txt";

                  System.IO.StreamReader objReader;
                  objReader = new System.IO.StreamReader(file\_name);
          
                  textBox1.Text = objReader.ReadToEnd();
          
                  objReader.Close();
              }
          
          D Offline
          D Offline
          Dan Mos
          wrote on last edited by
          #4

          Darrall wrote:

          string file_name = "C:/test1.txt";

          Because it works:). It transforms into "C:\\test1.txt"; But I don't. I use either

          @"C:\test1.txt";//or
          "C:\\test1.txt";

          D 1 Reply Last reply
          0
          • L loyal ginger

            Because it's easy to type! In this case the slash ("/") and backslash ("\") are both understood as the delimiter for path, so either one should work. However, backslash is used to escape in string literals, so to use backslash, you should escape it, like so: \\. You can see that is more typing than using just slash. If you have a lot of backslashes in your string literal, you can put @ in front of it so you don't need to escape them. For example,

            string file_name = @"C:\test1.txt";

            Still, there is the character @ you have to type. It's still more typing than simply using the forward slash.

            D Offline
            D Offline
            Darrall
            wrote on last edited by
            #5

            Thanks :)

            1 Reply Last reply
            0
            • J John Whitmire

              My guess is that it's simply a cheat to avoid having to type the requisite double backslash (special character):

              string file_name = "C:\\test1.txt";

              ...Byte conservation; it's one byte shorter in the source file. ;P

              D Offline
              D Offline
              Darrall
              wrote on last edited by
              #6

              Thanks :) That's the problem with using tutorials...nobody around to answer the howcomes.

              1 Reply Last reply
              0
              • D Dan Mos

                Darrall wrote:

                string file_name = "C:/test1.txt";

                Because it works:). It transforms into "C:\\test1.txt"; But I don't. I use either

                @"C:\test1.txt";//or
                "C:\\test1.txt";

                D Offline
                D Offline
                Darrall
                wrote on last edited by
                #7

                Thanks :)

                1 Reply Last reply
                0
                • D Darrall

                  Can anyone tell me why when writing code to open a file you use a forward slash in the path name instead of a back slash as in the following:

                  private void button1_Click(object sender, EventArgs e)
                  {
                  string file_name = "C:/test1.txt";

                          System.IO.StreamReader objReader;
                          objReader = new System.IO.StreamReader(file\_name);
                  
                          textBox1.Text = objReader.ReadToEnd();
                  
                          objReader.Close();
                      }
                  
                  A Offline
                  A Offline
                  Abhinav S
                  wrote on last edited by
                  #8

                  Reading this article might give you some more information.

                  Now...bring me that horizon. And really bad eggs...Drink up me hearties, YO HO!

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups