Link to home
Start Free TrialLog in
Avatar of Ashwin_shastry
Ashwin_shastry

asked on

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
Avatar of aacool
aacool

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.
ASKER CERTIFIED SOLUTION
Avatar of aacool
aacool

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Did this work? Please close out if it did
Avatar of Ashwin_shastry

ASKER

Good ...It works...Thank you.