1

Based on Opening Word document within C# that has spaces in the path. I'd like to ask how to do the same but when the path is acquired from list box. I know that if I use @"path" it will work, but how do I do the same to listBox.SelectedItem.ToString() when opening

var _p = new Process();
_p.StartInfo.FileName = "Word.exe"
_p.StartInfo.Arguments = lbFiles.SelectedItem.ToString();

Let's say I want to open "C:\new word document.docx". Word gives an error can't find path "C:\new.doc" any ideas how to do it.

Community
  • 1
  • 1
Light_User
  • 83
  • 2
  • 11
  • `"Word.exe"` is just for **presentation** the valuer is taken from registry baseed on this [post](http://stackoverflow.com/questions/9540051/is-an-application-associated-with-a-given-extension/9540278#9540278) – Light_User Oct 12 '14 at 02:13

1 Answers1

1

Try surrounding the file name in quotes as well:

_p.StartInfo.Arguments = string.Format("\"{0}\"", lbFiles.SelectedItem);

Also, Word.exe doesn't work on my system. You may need to change that to:

_p.StartInfo.FileName = "WinWord.exe"
Grant Winney
  • 65,241
  • 13
  • 115
  • 165
  • An unhandled exception of type 'System.InvalidOperationException' occurred in System.dll Additional information: Cannot start process because a file name has not been provided. – Light_User Oct 12 '14 at 01:59
  • "\"C:\\Users\\User\\Desktop\\New Microsoft Word Document.docx\"" – Light_User Oct 12 '14 at 02:03
  • this is the result I get ( with your solution ) – Light_User Oct 12 '14 at 02:04
  • I take the _p.StartInfo.FileName - from registry based on this [post](http://stackoverflow.com/questions/9540051/is-an-application-associated-with-a-given-extension/9540278#9540278) – Light_User Oct 12 '14 at 02:09
  • so `WinWord.exe` is not relevant but the path string should be some way to manipulate it – Light_User Oct 12 '14 at 02:10
  • positive 100% it gets to the list from open file dialog. also it breaks the name of the file and tries to search for each part.like new.doc microsoft.doc etc ... each space is a different search – Light_User Oct 12 '14 at 02:20
  • Also it works for `New Text Document.txt` and it's opened automatically in the notepad ( as defined by the registry ) as I said it's strange and it occurs only with WORD. – Light_User Oct 12 '14 at 02:25
  • I'll humor you more then you think, tried `"WinWord.exe"` - worked !! after changed back to the registry - **Worked also :)** – Light_User Oct 12 '14 at 02:34
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/62906/discussion-between-light-user-and-grant-winney). – Light_User Oct 12 '14 at 02:37