[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

process argument contains space in filename

Posted on 2004-11-21
4
Medium Priority
?
664 Views
Last Modified: 2012-06-27
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
0
Comment
Question by:Ashwin_shastry
  • 3
4 Comments
 
LVL 3

Expert Comment

by:aacool
ID: 12640988
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
 
LVL 3

Accepted Solution

by:
aacool earned 600 total points
ID: 12640995
Basically, the trick is to enclose the full path with " "
0
 
LVL 3

Expert Comment

by:aacool
ID: 12655630
Did this work? Please close out if it did
0
 

Author Comment

by:Ashwin_shastry
ID: 12656806
Good ...It works...Thank you.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Loops Section Overview
Suggested Courses
Course of the Month19 days, left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question