使用打开文件对话框在C#中引用反斜杠 [英] Quote backslash in C# with open file dialog

查看:185
本文介绍了使用打开文件对话框在C#中引用反斜杠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个非常简单的音频播放器


有一个按钮可以打开OpenFileDialog来选择要输入到listBox的MP3文件,效果很好,但是当我尝试在按钮上使用以下代码播放列表中的音频文件时:

I''m making a really simple audio player


There is a button which opens an OpenFileDialog to select an MP3 fileto input into a listBox, which works fine, but when I try to play an audio file in the list using this code on a button:

SoundPlayer sound = new SoundPlayer(listBox1.SelectedIndex.ToString());
sound.Play();



我收到一个错误消息,说该位置没有文件,所以我认为这是因为在Soundplayer中反斜杠必须为\\而不是\,但是openFileDialog中的FileName仅使用\,因为这就是它在Windows资源管理器中.
我尝试用\\替换\\但没有工作,但仍然抱怨(我什至尝试用\\替换\\以停止它所做的退出操作)

基本上我需要的是:

通过从列表框中获取名称来使按钮播放声音文件的方法

如果我不清楚,请让我重新措辞,我仍在学习,所以请尝试尽可能简单地解释.
谢谢!



I get an error saying there is not a file in that place, so I think it''s because the backslash has to be \\ instead of \ in the Soundplayer, but the FileName from openFileDialog only uses \ because that''s how it is in windows explorer.
I tried to string.replace \ with \\ but did not work it still complained (I even tried replace \\ with \\ to stop the exit thing it does)

Basically all I need is:

A way to make a button play a sound file by getting the name from a listbox

If I am unclear please ask me to rephrase, I am learning still so try explain as simple as possible.
Thank you!

推荐答案

在文本框或文件对话框之类的数据字段中,所有路径名均按原样读取,没有反斜杠转义或其他愚蠢的现象.由于C,C ++或C#语法,这种转义仅在源代码字符串文字中需要 .

但是,在过去,我看到了很多真正对大脑有害的C和C ++软件,即使在原始数据中也存在这种逃逸现象.您不会在.NET应用程序中看到类似的信息,除非…您知道很难消除白痴. :-)
无论如何,您的代码都不是这种情况.

您的问题的原因在其他地方.如果仍然找不到,则可以在调试器下运行它,或者在非常短的代码示例中重现该问题,然后将其发布在此处(使用上面的改进问题").

—SA
In the data fields like the text box or a file dialog, all path names are read as is, with not backslash escaping or other foolishness. Such escaping is only needed in source code string literals, due to C, C++ or C# syntax.

However, in the past I saw a lot of really brain-damage C and C++ software where such escapes present even in raw data. You should not see anything like that in .NET applications, unless… idiocy is hard to cure, you know. :-)
You code is not the case anyway, it looks like.

The reason of your problem is somewhere else. If you still did not find out, you can run it under debugger or reproduce the problem on a really short code sample which you can post here (using "Improve question" above).

—SA


这篇关于使用打开文件对话框在C#中引用反斜杠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆