I made an application in C#, where I open a virtual keyboard. I also have a Lua application that runs this keyboard, and both are functional. In this scenario I needed to send some information from Lua to C# by the command line, but I'm not having success, does anyone know if it is possible, and if so, how could I do it?
Code Lua-
os.execute('C:\Users\Public\Documents\Keyboard.exe')
The code just calls the executable.
Code C#-
namespace WindowsFormsApp2
{
static class Program
{
[STAThread]
static void Main(string[] args)
{
// Test if input arguments were supplied.
if (args.Length >= 0)
{
Console.ReadLine();
}
Console.WriteLine(args.Length);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Keyboard());
}
}
}
Here I read the console, but I'm not sending any code through Lua to read through C#, in this case I wanted to send a parameter through lua, and receive it through C#, but I don't know how to implement this through Lua.
Related
I am currently trying to debug an application and I am passing debug command line arguments via Visual Studio when starting the application as this is the only way available to me to debug this.
When I use either the > or < symbols in the command line arguments they are simply ignored. This does not happen when the application is called via command prompt however.
I have tried using 'ampersand' gt; , but this did not work. Can anyone please advise?
EDIT: The code I'm using is
Processor.CommandLineArgs = My.Application.CommandLineArgs
An example of it being used is me passing "/output.txt />3"
My.Application.CommandLineArgs has 2 items, "/output.txt" and "/3"
If your application is a console application simply store the values you receive directly from the Program.cs Main(string[] args) method. It will contain all you need.
If you are using a winform project you can simply edit the same file and also read the parameters from there instead. Here a simple example of a modified program.cs of a brand new winform project
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args) // just added the args here
{
var myParams = args; // read the values and do something. This conserve special characters
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
This question already has an answer here:
How to be dynamically either console application or Windows Application
(1 answer)
Closed 4 years ago.
I have this task given to me and have no idea how to approach it.. Everywhere online says that this isn't possible without .dll or mocking.
The server, when launched with an argument of –w should open a windowed interface that
permits an operator to control the functions of the server. If launched with no arguments it
should operate as previously specified in part 1
(part 1 is a console application)
I don't know if I'm missing something obvious.
Thanks for any help you can give
I don't know where you're seeing on-line that this isn't possible. Every Windows Forms application has a Main method, you just need to modify it a bit.
[STAThread]
static void Main(string[] args)
{
if (args.Length > 0)
{
File.WriteAllText("hello.txt", "foo");
}
else
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
I added the string[] args argument and then check it. I'm not checking for -w, I'm just checking for any old argument, but you should be able to take it from there.
It's absolutely not impossible. Just add reference to System.Windows.Forms, add it in the using clause, and go from here.
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Linq;
namespace ConsoleWindow
{
class Program
{
static void Main(string[] args)
{
if (args.Contains("-w"))
{
Form1 f1 = new Form1();
f1.ShowDialog();
}
Console.ReadKey();
}
}
}
I'm sure there's some simple answer, but none of the other Stack Overflow posts has helped me. My code will not log to the Console, and it's hard to do anything useful with that state of affairs.
using System;
using System.Diagnostics;
namespace Learning
{
class MainClass
{
public static void Main (string[] args)
{
Debug.Log ("this?");
Debug.Print ("How about this?");
Console.WriteLine ("WORK");
Console.ReadLine ();
}
}
}
I've been able to write to the console before, I don't know why it's being persnickety now.
Probably because your code doesn't actually compile. Log() is a static method of Debugger, not Debug, and it takes three arguments: level, category, and message.
public static void Main (string[] args)
{
System.Diagnostics.Debugger.Log(1, "category", "this?");
System.Diagnostics.Debug.Print ("How about this?");
Console.WriteLine ("WORK");
Console.ReadLine ();
}
It's worth noting that Debug/Debugger methods will not do you any good unless you are Debugging. To start a debugging session in mono, go to the Run -> Debug
You may want to check what kind of application you are using. For example, if you are making a Forms Application, you won't have access to the Console functions.
You can change this by going into the Solution Properties, and changing it from a Windows Forms Application to a Console Application. This won't have any effect on your program, other than it will run a Console alongside.
I have a simple question and I'm sure it's been answered, but I can't seem to find the solution I'm looking for.
My basic question is that I've created a console app in .Net that runs automatically on a task scheduler every day, but now my clients also want a windows form-based interface that they can use to run special runs (they use the GUI to specify a few parameters - such as specific dates, etc - to make the program run a bit differently).
The way I thought to do this would be to convert my console app to a WinForm solution and to include Command Line Arguments for when I wanted it to run as the original console app with defaults, but I'm thinking that's not the right way since it would still involve a form load.
My other thought was to convert the "engine" part to a DLL and to make 2 executables - One a console app and one a winforms app, but I'm not sure if that's right either / could lead to more maintenance.
What is the correct way to do this?
I'm writing this in VB, but am equally comfortable with C# solutions if that is easier for you.
Thanks!!
Typically, I'd split the logic into a library, then make a simple console app and a simple Forms app to call into the logic in the library.
You can then distribute these separately and use them as they are intended, without duplication of code.
You can modify your Program.cs to accept arguments, then if some args had been passed to your app prcess them and exit from main, else start your main form, something like this:
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
if (ProcessCommandLine(args))
return;
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
static bool ProcessCommandLine(string[] args)
{
//Process it, if some has been processed return true, else return false
}
}
Or you can make the form invisible and go with your first option, reading the commandline.
here is a way of making the invitial form invisible. Form Invisible
Three projects as Reed suggested is a correct approach, however if you really need 1 executable (which we for some reason really wanted in one case) you can do following:
static class Program
{
[STAThread]
static void Main(string[] args)
{
if (args[0]== "-ui")
{
System.Windows.Forms.Application.EnableVisualStyles();
System.Windows.Forms.Application.SetCompatibleTextRenderingDefault(false);
System.Windows.Forms.Application.Run(new MyFormWork());
}
else if (args[0] == "-console")
{
Helper.AllocConsole();
DoYourConsoleWork();
Helper.FreeConsole();
}
}
public static class Helper
{
[DllImport("kernel32.dll")]
public static extern Boolean AllocConsole();
[DllImport("kernel32.dll")]
public static extern Boolean FreeConsole();
}
1: you can use XML for arguments. your console just need to read XML for its argument.
then create a form app just for editing XML and save.
Benefit:
your app is running by task scheduler so your form do not need to .
for user it's easy to open form app and change something that will save to xml
Console --run every day without any notice
Argument.xml -- argument for Console .
Form -- user interface
2: you can mix both within a form but it will run every day in form base not good idea
I can find many examples on how to get arguments in a console application, but I can't seem to find an example of how to get arguments in a windows form application.
I would like to following things.
whenever I open a jpg file, windows launches my application.
I would like to know path and name of the jpg file from my application.
How do i do that?
Environment.GetCommandLineArgs
Open up program.cs, on a file > new > winform project, you'll get
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
change this to
static class Program
{
[STAThread]
static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
Now its just like the console apps, you'd access them via args.
Even if you don't go with this option, you should be aware of how the win form app is initialized :) This way, you could run different forms or not run a form at all.