Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
500 views
in Technique[技术] by (71.8m points)

c# - Replace double backslashes with single backslash

I have created a search function within a media player that uses a list-box to output the data and when a user clicks a song I want that song to play. However, when I select a song for some reason I get two black slashes instead of one. Please help. I've tried replacing them already.

string path = @"C:UsersUsernameMusic";
        path = path.Replace(@"", @""); 
        string selectedsong, filetoplay;

        selectedsong = listBox1.SelectedItem.ToString();

        filetoplay = path + selectedsong + ".mp3";

        Form1.wplayer.URL = filetoplay;

What I'm currently getting at the moment is C:\Users\Username\Music\Song.mp3 and as a result the song won't play

The file paths after I click in the listbox

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

That Replace in your code is doing nothing because there are no double backslashes in your string... As others have already pointed out, it's just a matter of debugger visualization so you can copy that and use it in your code, for example.

So, if you do this:

code

You see the double backslash there but it's not in the actual string, of course. See what you get in the console:

Console output

My advice is that you simulate a double click on the selected file by running this:

System.Diagnostics.Process.Start(filetoplay);

It should open your default mp3 player. I think it will give you an error due to missing file, wrong format or something. If it plays, then the bug is on the player part of your code and you can stop worrying about the double slashes. :)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...