locked
Open a file with Shell - Returns error on correct filepath.

    Question

  • I need to open a *.py file in the shell (DOS) box, but it returns an error that no file was found at the specified path. But this path is completely correct.
    Unless Visual Basic are referencing to paths in another matter than C#? 'Cause i am developing in C# and referenced to VB to do this.

    Here's my code:
    Interaction.Shell("C:\\Users\\" + Environment.UserName + "\\Desktop\\hej.py", AppWinStyle.NormalFocus, false, -1);
    Why does it say it couldn't find the file ("hej.py") when it is right there, on the desktop?
    Please help me :)
    Monday, March 15, 2010 7:01 PM

Answers

All replies

  • Replace the double slashes with single ones.
    Interaction.Shell("C:\Users\" + Environment.UserName + "\Desktop\hej.py", AppWinStyle.NormalFocus, false, -1);
    


    Hannes

    If you have got questions about this, just ask.
    Mark the thread as answered if the answer helps you. This helps others who have the same problem !
    C# to VB.NET: http://www.developerfusion.com/tools/convert/csharp-to-vb/
    Monday, March 15, 2010 7:13 PM
  • Shell cannot open file type directly unless you specify the application (executable file) that will open it.  For example, to open .txt file using shell, you have to tell shell that the file has to be open by notepad or any other text editor. 

    This is an example: open code.py using python.exe
     Interaction.Shell("C:\\Python30\\python.exe" + " " + "C:\\Python30\\Lib\\code.py", AppWinStyle.NormalFocus, false, -1);

    You can read more about shell http://msdn.microsoft.com/en-us/library/microsoft.visualbasic.interaction.shell.aspx

    so try system process which automatically open file with default application.
    string fileToOpen = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory).ToString() + "\\hej.py";
     System.Diagnostics.Process.Start(fileToOpen);

    kaymaf


    CODE CONVERTER SITE

    http://www.carlosag.net/Tools/CodeTranslator/.

    http://www.developerfusion.com/tools/convert/csharp-to-vb/.

    • Marked as answer by Bums12 Wednesday, March 17, 2010 2:36 PM
    Tuesday, March 16, 2010 5:42 AM
  • so try system process which automatically open file with default application.
    string fileToOpen = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory).ToString() + "\\hej.py";
     System.Diagnostics.Process.Start(fileToOpen);

    This works thank you, how can i prevent the shell from shutting down at end of script in Python? I know it is irrelevant to the topic, but if you knew - i'd be very happy :) I've googled for hours and many has the problem but no solutions. Please try :)

    Thank you :)
    Wednesday, March 17, 2010 2:35 PM