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. Backslashes and forward slashes

Backslashes and forward slashes

Scheduled Pinned Locked Moved C#
tutorialquestion
13 Posts 7 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

    I am retrieving a filepath that I need in my program using a folderBrowserDialog. Can anyone tell me how to change all the back slashes to forward slashes? Thanks Darrall

    OriginalGriffO D 2 Replies Last reply
    0
    • D Darrall

      I am retrieving a filepath that I need in my program using a folderBrowserDialog. Can anyone tell me how to change all the back slashes to forward slashes? Thanks Darrall

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #2

      string withForwardSlashes = withBackwardSlashes.Replace('\\', '/');

      or

      string withForwardSlashes = withBackwardSlashes.Replace("\\", "/");

      You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      D 1 Reply Last reply
      0
      • OriginalGriffO OriginalGriff

        string withForwardSlashes = withBackwardSlashes.Replace('\\', '/');

        or

        string withForwardSlashes = withBackwardSlashes.Replace("\\", "/");

        You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy

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

        I tried it exactly as you have it there and I get "Too many characters in character literal" and "No overload for method 'Replace' takes '1' arguments." Any thoughts? Thanks for your time. Darrall

        OriginalGriffO R 2 Replies Last reply
        0
        • D Darrall

          I am retrieving a filepath that I need in my program using a folderBrowserDialog. Can anyone tell me how to change all the back slashes to forward slashes? Thanks Darrall

          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #4

          Darrall wrote:

          Can anyone tell me how to change all the back slashes to forward slashes?

          For what purpose? In Windows, the standard path seperator is a backslash while the forward slash is used for command line switches.

          A guide to posting questions on CodeProject[^]
          Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
               2006, 2007, 2008
          But no longer in 2009...

          D 1 Reply Last reply
          0
          • D Dave Kreskowiak

            Darrall wrote:

            Can anyone tell me how to change all the back slashes to forward slashes?

            For what purpose? In Windows, the standard path seperator is a backslash while the forward slash is used for command line switches.

            A guide to posting questions on CodeProject[^]
            Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                 2006, 2007, 2008
            But no longer in 2009...

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

            I am using folderBrowserDialog to locate the folder where the user stored his program which also has the location of the data files which are needed for the program. The path is returned as it is in the computer with backslashes but can't be used that way by the program.

            P 1 Reply Last reply
            0
            • D Darrall

              I tried it exactly as you have it there and I get "Too many characters in character literal" and "No overload for method 'Replace' takes '1' arguments." Any thoughts? Thanks for your time. Darrall

              OriginalGriffO Offline
              OriginalGriffO Offline
              OriginalGriff
              wrote on last edited by
              #6

              Works fine for me - could you post the code fragment exactly as you have it?

              You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy

              "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
              "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

              D 1 Reply Last reply
              0
              • D Darrall

                I tried it exactly as you have it there and I get "Too many characters in character literal" and "No overload for method 'Replace' takes '1' arguments." Any thoughts? Thanks for your time. Darrall

                R Offline
                R Offline
                Ravi Bhavnani
                wrote on last edited by
                #7

                Darrall wrote:

                Any thoughts?

                Yes, two:

                • Did you type carefully?
                • Post your code if you want us to help you fix it.

                /ravi

                My new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com

                1 Reply Last reply
                0
                • OriginalGriffO OriginalGriff

                  Works fine for me - could you post the code fragment exactly as you have it?

                  You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy

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

                  Here it is :)

                  private void button2_Click(object sender, EventArgs e)
                  {
                  string OldPathName;
                  string NewPathName;

                          if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
                          {
                              MessageBox.Show("MyFolder = " + folderBrowserDialog1.SelectedPath);
                              textBox1.Text = folderBrowserDialog1.SelectedPath;
                  
                              OldPathName = NewPathName.Replace ('\\','/');
                          }
                  
                  F OriginalGriffO 2 Replies Last reply
                  0
                  • D Darrall

                    Here it is :)

                    private void button2_Click(object sender, EventArgs e)
                    {
                    string OldPathName;
                    string NewPathName;

                            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
                            {
                                MessageBox.Show("MyFolder = " + folderBrowserDialog1.SelectedPath);
                                textBox1.Text = folderBrowserDialog1.SelectedPath;
                    
                                OldPathName = NewPathName.Replace ('\\','/');
                            }
                    
                    F Offline
                    F Offline
                    FyreWyrm
                    wrote on last edited by
                    #9

                    Backslash is an escape character. The '\' you entered is interpreted as ''. You can change that to @'\' or '\\' and it should work.

                    Don't blame me. I voted for Chuck Norris.

                    D 1 Reply Last reply
                    0
                    • F FyreWyrm

                      Backslash is an escape character. The '\' you entered is interpreted as ''. You can change that to @'\' or '\\' and it should work.

                      Don't blame me. I voted for Chuck Norris.

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

                      Thanks :) Somewhere along the line I think C# should have left the backslash alone.

                      L 1 Reply Last reply
                      0
                      • D Darrall

                        Thanks :) Somewhere along the line I think C# should have left the backslash alone.

                        L Offline
                        L Offline
                        Lost User
                        wrote on last edited by
                        #11

                        Using an other character would probably be worse, since so many* languages use the backslash as escape character.. * Perl, Python, Java, ECMAScript, C, C++, PostScript, RTF, awk, sh and many others

                        1 Reply Last reply
                        0
                        • D Darrall

                          I am using folderBrowserDialog to locate the folder where the user stored his program which also has the location of the data files which are needed for the program. The path is returned as it is in the computer with backslashes but can't be used that way by the program.

                          P Offline
                          P Offline
                          PIEBALDconsult
                          wrote on last edited by
                          #12

                          Darrall wrote:

                          can't be used that way by the program

                          Fix the program.

                          1 Reply Last reply
                          0
                          • D Darrall

                            Here it is :)

                            private void button2_Click(object sender, EventArgs e)
                            {
                            string OldPathName;
                            string NewPathName;

                                    if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
                                    {
                                        MessageBox.Show("MyFolder = " + folderBrowserDialog1.SelectedPath);
                                        textBox1.Text = folderBrowserDialog1.SelectedPath;
                            
                                        OldPathName = NewPathName.Replace ('\\','/');
                                    }
                            
                            OriginalGriffO Offline
                            OriginalGriffO Offline
                            OriginalGriff
                            wrote on last edited by
                            #13

                            I take it from the other responses that that problem is fixed? Now change your code slightly to make it actually work!

                            private void button2_Click(object sender, EventArgs e)
                            {
                            string OldPathName;
                            string NewPathName;

                                    if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
                                    {
                                        MessageBox.Show("MyFolder = " + folderBrowserDialog1.SelectedPath);
                                        NewPathName = folderBrowserDialog1.SelectedPath;
                                        textBox1.Text = NewPathName;
                                        OldPathName = NewPathName.Replace ('\\\\','/');
                                    }
                            

                            You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy

                            "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                            "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                            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