How do i get input from "textbox1" to a .bat file

Viewed 387

I'm new to programming and I want to learn with c# for now, I want to make a program that take the input "the user" put in "textbox1" in to my test.bat file

Text in "test.bat"

Set A="textbox1.text" 
Echo %A%

Here's the image of the ui in the visual basic I wrote with the textbox

With the "button1" click it will start the test.bat and show the textbox1 text in the cmd if its possible

I would love to get it to work.

Thanks for the help !!

3 Answers

Your desires can be met with a couple of lines of code employing the File.WriteAllText to create the file with its contents and the Process.Start method to run it. Ctrl-F on that page for /k if you're having trouble getting your console window to remain open to actually see the message

I'll leave the task of formulating the string contents to be written to the file, to you. Remember that the documentation usually has examples on how to use every method it describes so a ready source of pre written code is available

Final tip; you're allowed to rename controls after you drop them on a form, and you really should take the 2 seconds needed to do so, so that you aren't one day asking us for help with code that is full of label27, textbox56 - it makes a program far harder for everyone to understand (look up what "obfuscation" is), including you, and is nonsensical when it's so easy to rename them to batchFileMessageTextBox or saveAndLaunchBatFileButton

To my understanding,you want to use C# to create a dialog box to take user input and show it in console,well its not possible directly but you can store the input given by user in a file and then use a batch file to read it.i am not a c# programmer so i don't know how to work with dialog box in c# but the Following link provides your solution and then use the following batch file to read the input in file:

@echo off
set /p var=<file.txt
echo %var%
pause >nul

There are a couple of simple approaches to this. One is to do what you asked; to write the input text to a file named "textbox1.text" (really should be "textbox1.txt", though, since ".txt" is a common suffix for text files and ".text" isn't) and then let the batch script read it.

It's a whole lot easier to simply write the text to the standard output stream using Console.WriteLine() and let the batch file capture that and do what it wants with the text. Either way, you're going to need a character that's known not to ever be an input character.

I'll give the second approach as an example, but I won't use Forms or WPF. It's too hard to show what goes where. You seem to want a window with a text box, though, so I'll use the following "ConsoleApp1.cs" program and borrow the InputBox() function from Visual Basic (a neat console CS trick on its own!):

using System;
using Microsoft.VisualBasic;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            string answer = Interaction.InputBox("Tell me something");
            Console.WriteLine(answer);
        }
    }
}

Compile that into a .exe file and copy it to the directory you're using for running .bat files. Now try the following within a batch file:

rem @echo off
for /F "delims=~" %%t in ('ConsoleApp1.exe') do set a=%%t
echo.%a%

That's the easy way to get one line of text from a CS program into the .bat file that called it. If you insist on having the CS program create a text file named "textbox1.text", go ahead. Look up how to use the StreamWriter class to do that. Then your .bat file will use a slightly different form of the FOR command:

for /F "delims=~" %%t in (textbox1.text) do set a=%%t

Either way, you need the character after delims= to be a delimiter character that will not by typed by the user. The FOR /F command always parses input lines into tokens separated by whatever delimiter characters you specify (or spaces by default). I use either ~ or ' as characters that don't normally show up in commands or file names.

Justaus3r made a valuable suggestion to use a redirected SET /P command. You can use that, with the above CS program to get the Console.WriteLine output into a variable with no delimiter worries:

ConsoleApp1.exe >textbox1.text
set /P a=<textbox1.text

That works. Piping output from ConsoleApp1 directly to SET ought to work too, but for some reason it doesn't. I've had a number of issues with old batch files that worked in Win7 that don't in Win10. This fails in both CMD and PowerShell, though, so there's something weirder going on that MS just isn't maintaining CMD any more.

Related