Visual Studio and Batch (OpenFileDialog) RRS feed

  • Question

  • Hi

    Im using a batch script which run correctly and im using OpenFileDialog (and FolderBrowserDialog), so those command worked just by executing the batch file. But when i try to run the batch file from visual studio it's like it doesn't recognise OpenFileDialog (and FolderBrowserDialog) because there aint no more Windows to select my .exe or my folders. It just do Nothing.
    For the moment I am using the command : 


    So the CMD is launch and the "echo" worked cause i can see some messages but not the Windows which should open with OpenFileDialog…

    Any ideas ?

    Sorry for the faults of languages, but i hope that there will be more answers in English than in FRench… :)  

    • Edited by Leo_Info Monday, June 4, 2018 2:19 PM
    Monday, June 4, 2018 2:15 PM

All replies

  • Your batch file is probably wrong, it should contain the exe from the windows you would like to open. 

    Can be as simple as a wrong folder


    Monday, June 4, 2018 3:50 PM
  • What? Batch files aren't designed to automate UIs. What exactly are you trying to accomplish? Can we see your script so we can see what you're doing? Console apps don't use OpenFileDialog or any Win32 UI as it isn't a Windows UI app. The best you can do is trigger a shell call.

    Michael Taylor http://www.michaeltaylorp3.net

    Monday, June 4, 2018 6:02 PM
  • I m trying to backup the database, Im on a traineship and my boss wanted me to a backup with a batch script and make an interface to execute this batch. 

    This code should work partialy (I have modify some things for informations and translation) : 

    @echo off

    echo Backup your database ?
    set /p choix=(O/N)
    if NOT %choix%==o GOTO sinon2
    set "tempfile=%temp%\file-%random%"
    if exist "%tempfile%" goto :loop

    call :getfile "c:\"
    for /f "delims=" %%a in ('powershell "%tempfile%.ps1" ') do (
       set "filepath=%%~dpa"
       set "filename=%%~nxa"
    del "%tempfile%.ps1"
    echo Please wait for the server name...
    sqlcmd -Lc
    echo Choose backup folder

    set "tempfile2=%temp%\file-%random%"
    if exist "%tempfile2%" goto :loop2

    call :getfile2 "c:\"
    for /f "delims=" %%a in ('powershell "%tempfile2%.ps1" ') do (
       set "path=%%~fsa"
    del "%tempfile2%.ps1"

    SET /p serveur=Enter your server name: &;

    echo Please wait...
    "filepath%""%filename%" /Gui=FALSE "/BackupAll="%path%"\POMAREDE.zip;providerid=**|data source=%serveur%\** |user id=**|encryptedpassword=**|networklibrary=Dbmslpcn";**;
    echo $initialDirectory = "%~1"
    echo [System.Reflection.Assembly]::LoadWithPartialName^("System.windows.forms"^) ^| Out-Null
    echo $FolderBrowserDialog = New-Object System.Windows.Forms.FolderBrowserDialog
    echo $FolderBrowserDialog.initialDirectory = $initialDirectory
    echo $FolderBrowserDialog.ShowDialog^(^) ^| Out-Null
    echo $FolderBrowserDialog.SelectedPath
    ) > "%tempfile2%.ps1"

    echo $initialDirectory = "%~1"
    echo [System.Reflection.Assembly]::LoadWithPartialName^("System.windows.forms"^) ^| Out-Null
    echo $OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
    echo $OpenFileDialog.initialDirectory = $initialDirectory
    echo $OpenFileDialog.filter = "Fichier .exe(*.exe*)| *.exe*"
    echo $OpenFileDialog.ShowDialog^(^) ^| Out-Null
    echo $OpenFileDialog.filename
    ) > "%tempfile%.ps1"
    goto :EOF

    I am not use to batch files i ve been using it since a Week now …

    So maybe i've done Something wrong by using OpenFileDialog on my batch but i've seen that it was possbile to use it in some forums.

    sqlcmd -Lc won't probably worked on your computers. But i hope that you will be able to see the Windows dialog

    Monday, June 4, 2018 9:00 PM
  • I see you're mixing a command script with powershell but you posted in the C# forums. If you can use Powershell then I would stick with that and forego C# or a command script. It would be dramatically simpler. However your best option at that point is to post in the TechNet Powershell forums where you can get some helper scripts.

    As for the original question of the file dialog I wouldn't really recommend that approach. The biggest benefit of a script is that it can be automated so everything should really be driven by the command line. For example your script should require that the necessary parameters be specified. In Powershell you can configure it to optionally prompt for any parameters that are missing.  But if the user provides them then you don't prompt. You can even default parameters if they are not specified to make it easier.

    As an example of what appears to be your batch file flow. Have a parameter that specifies whether to back up or not. Default to yes and then use additional parameters to provide the data. If you need a file then prompt for it directly without using a dialog. This is a script after all. If a user wants to be able to use the UI then they can open WIndows Explorer themselves and copy/paste the file/path into the script. Because it is powershell you can call into .NET directly to get any additional information (or validation) such as the databases available, credentials etc. All these are parameters that can be specified as well. When you're finally ready you can rely on the -WhatIf special parameter to either log the changes you would make or actually make the changes.

    Powershell ISE is the editor used for editing Powershell and it comes with Windows. You can easily debug your scripts through it. This makes Powershell the best option for scripting stuff.

    Michael Taylor http://www.michaeltaylorp3.net

    Tuesday, June 5, 2018 12:58 AM
  • Thanks for your answer and your advices.
    Im going to post in powsershell section. I've come here because even if my batch seems odd… :) it works so the problem was just when I've been using C# to execute the batch file. (When i defined a path it works when i execute it with C#)

    Is there another way to have the file dialog ? And i'll search for interface with PowerShell if it can be more simple.

    However thanks for your helps :)

    • Edited by Leo_Info Tuesday, June 5, 2018 7:53 AM
    Tuesday, June 5, 2018 7:21 AM
  • Why do you need to run a batch file from the C# file? Can you not just code everything in C#? If you can then you can use OpenFileDialog directly in C# and forego the batch file altogether.

    If you need to use a batch file then use Powershell. In Powershell you can use OpenFileDialog as well. The folks in the Powershell forum can help you with that as it isn't C#.

    Michael Taylor http://www.michaeltaylorp3.net

    Tuesday, June 5, 2018 1:46 PM