Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
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 !!
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
I want to right-click on a text file and "Open With..." it with my own program, but I can't find any information on how to do that. I want to make my program in C++ or with WinForms (C#).
I want to open that file and to use my program as an interpreter on a small "homemade programming language", so I want to pass the data from the file directly to my program.
Can anyone help me?
*hope I'm clear enough on what I'm trying to do.
I'm just gonna to answer your Question for C#. If you still need C++ support you can tell me.
Option 1 - Drop down:
So if you for example create a Console-Application in C# (Visual Studio), it will look like this:
As you can see in the Picture: the Program accepts Arguments (args String Array)
If you drag & drop your file on your .exe, the filepath of the file you dropped will be saved in the args String Array. Now you can read the file (for example with the File-Class).
Option 2 - Right Click -> Open with my Program:
For that, you can simply add a new entry in HKEY_CLASSES_ROOT\Directory\Background\shell (Windows Registry) to register you Program as a "Right Click Menu Program".
Here is a detailed How-To:
https://www.howtogeek.com/howto/windows-vista/add-any-application-to-the-desktop-right-click-menu-in-vista/
After you added your Program to the Windows Registry you can proceed as shown in Option 1 (args).
Any more questions? Let me know.
Greets
Bennet
EDIT:
Sorry, didnt really read the comments :D but i guess your Question is answered. I will let this stay here for future readers which dont read the comments either ;)
I'm trying to work on an environment that its main function is to adopt Visual Programming to create NetLogo code (similar to Google's Blockly).
Right now, I'm using Unity3D to do the job and wondering if it's possible to access NetLogo from it. The objective is to send the generated code directly into the Code Tab, opening a blank project already with the code in the tab (without the user copying and pasting it there).
What I know up until now is that I can open NetLogo from Unity with a function called Process.Start, which takes 2 arguments: the first is the name of the target program to be executed ("NetLogo.exe"), the second one is a list of arguments that can be passed to the targeted program, which solely depends on each program, as found here and here. However, I didn't understand much about these arguments, which is why I recurred to ask.
Do I need to also work on a Java/Scala environment to do this for me with the Extensions API, or can I use these arguments in Process.Start to do it?
Thanks in advance.
You could create a fully formed .nlogo file (it's basically a text file with a specific format), and then launch NetLogo using your Process.start command with that filename as an argument so that NetLogo will open that specific file.
You could even create a .nlogo file as a template (with whatever interface items you want), and then use string search/replace to substitute in the code that you want in the code tab.
Alternatively, fancier things are possible with the Controlling API , but I don't know much about calling JVM code from within Unity, and I suspect that will be a bigger headache than you want... unless you really need a more tight-knit connection to NetLogo, or unless the performance overhead of starting a new NetLogo process each time is unacceptable.
I'm working at the moment with a PowerShell script calling a C# console application, but I'm curious if there's a general solution.
Basically, my script loops over some files, prints out something for each one, and calls my program. Everything works, but what I'd like to do is have my console program's output show up indented by one tab stop. Right now I've managed that manually (just "hard-coding" the tab stop in my Console.WriteLine() statements), but that's hardly ideal from a maintenance standpoint, since sometimes this program will be run on its own.
An example of what I'm looking for is
powershell output file #1
c# program output
c# program output
powershell output file #2
c# program output
c# program output
Would it help to use a dedicated logger class? It would also be slick to do different colors for the called program, but I'd settle for a non-intrusive way to do indentation. In particular, a solution that avoids the necessity of editing the called program (e.g. for third party code I don't have source for) would be preferred.
Here's a pure PS solution:
# This assumes all output you care about is via stdout
$yourProgramOutput = yourprogram.exe
$yourProgramOutput | foreach-object {
write-host "`t$_"
}
or as a one-liner:
yourprogram | % { write-host "`t$_"}
I am writing a launcher in C# that processes user input and I would like its corresponding output string to be piped into an external program. The external program is a precompiled C# executable that is end-of-life, so I cannot easily modify it.
I can easily launch the desired program using ExternalProcess, but do not see any means for sending data over. My desired function calls go something like:
string myString = "string to pass over"
Clipboard.SetText(myString);
Process ExternalProcess = new Process();
ExternalProcess.StartInfo.FileName = "Notepad.exe";
ExternalProcess.Start();
// some kind of paste command here...
// ExternalProcess.magicpaste(myString);
ExternalProcess.WaitForExit();
Is there a better way to go about this? I realize the purported security implication in piping text this way, but I need to create a simple launcher program to support a legacy project.
Given your answer in the comments that you need to put some text in a field, I'm afraid you're going to have to get your hands dirty.
That text box (I assume that's what it is) in this legacy application, will be a window. And you're going to have to find it, and send it a message using Platform-Invoke. This question should get you started.
If said control has a name, you can find it using Spy++.
I am new to ASP.NET and I have a challenge here:
I have to create a webpage which should take C/C++ program from textarea and should produce output of the program below the textarea using label when a button is clicked on the webpage.
I have no difficulty creating the front page(textarea,button,label or other controls) and reading the content of textarea in back end page(cs page).
But I don't have any idea how I can compile and run the program read from textarea and produce the output which can be assigned to a label on webpage.
Any help on this will be much appreciated.
Thanks
You can use
System.Diagnostics.Process.Start()
http://msdn.microsoft.com/en-us/library/system.diagnostics.process.start.aspx
followed by
Process.WaitForExit
to run the Visual C++ compiler (CL.EXE). The documentation for CL.EXE is here: http://msdn.microsoft.com/en-us/library/610ecb4h.aspx
Your needs might be as simple as:
c:\path-to-compiler\cl.exe file.cpp /clr
In particular, I'd recommend that you review all your choices for the /clr option: http://msdn.microsoft.com/en-us/library/k8d11d4s(v=VS.80).aspx
You need to have Visual C++ on your machine.
I assume you only want to treat single source file programs using only standard libraries. I also assume that you are working on Windows (asp.net). As soon as you have program source text, you can save it to a file. Then you start cl as an external program with your source as parameter to compile it. And finally you start the program you have just compiled. Of course, you will have to give pipes for Stdout and probably stdin and stderr.