process argument contains space in filename

Hi everyone:

I want to play a song by clicking a button. For example if i want to play song using windows media player here is the code....On click of play button

Process myProcess = new Process();

myProcess.StartInfo.FileName ="wmplayer";

myProcess.StartInfo.Arguments = @"C:\bin\pleaseforgiveme.mp3";

myProcess.StartInfo.WindowStyle = ProcessWindowStyle.Normal;

myProcess.Start();

This works fine. The problem i am having right now is if the file name is  C:\bin\Please forgive me.mp3 i.e if it has whitespaces in the file (space b/w please forgive me) it does n't play the file. Or if it has underscore(_) in file name like please_forgive_me it does not play file. It throws exception. Could any of you help me in this ?

Thanks and with regards

Ashwin
Ashwin_shastryAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

aacoolCommented:
The problem is that escaping the string with @ actually does not escape the special characters like spaces within the string.

One approach is to explicitly escape special characters within the string with \

For example,

void Button2Click(object sender, System.EventArgs e)
{
      try
      {
         Process myProcess = new Process();
        myProcess.StartInfo.FileName ="wmplayer";
        myProcess.StartInfo.Arguments = "\"d:\\dnload\\music\\01 - I'm A Mover.mp3\"";
        MessageBox.Show(myProcess.StartInfo.Arguments.ToString());
        myProcess.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
        myProcess.Start();
      }
      catch(Exception ex)
      {
            MessageBox.Show(ex.Message+ex.StackTrace);
      }
}


This works just fine and plays the song.
0
aacoolCommented:
Basically, the trick is to enclose the full path with " "
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
aacoolCommented:
Did this work? Please close out if it did
0
Ashwin_shastryAuthor Commented:
Good ...It works...Thank you.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.