Looking how to run the program.cs file from a winform as I'm trying to run it, hide it and display the text in the textbox but I cannot find a way to run the console separately once the winform has started up!
A .cs file is just plaintext as far as I'm aware - You probably need to to actually be executable - From there you can use IPC to communicate between the two processes.
If you have somehow made the program.cs run (Black magic?) you could also get one process to write to a text file, then get the "winform" process to read from it (And then delete it!).
You might try elaborating on your problem, method and environment in your question - There's not a lot to work off here.
Make a console application and add a WinForm to do. Show the winform with .ShowDialog() and you will have both the console and the winform showing at the same time.
class Program
{
static void Main(string[] args)
{
var dlg = new Form1();
dlg.ShowDialog();
}
}
Related
If I try to open Notepad from a .NET console application it works fine.
I'm doing it as follows:
var p = new Process
{
StartInfo =
{
FileName = "c:\windows\system32\notepad.exe";
}
};
p.Start();
When I try to open the application I actually want to open, nothing happens. If I open that application by hand I see a Java process being created, which means it's a Java application packaged as an exe file.
Any ideas on how to open Java exe apps through .NET?
Thanks in advance
There shouldn't be much of a difference between a regular EXE and an "exified" Java application. Have you tried adjusting the working directory? Maybe there's some unzipping going on.
Lets say you had an application named "HelloWorld". Then from a command prompt (and from your code) you would launch it with:
java HelloWorld
Now this would assume that java is in your path. Which is a bad assumption. So you would be better off either having logic to populate the path to java, or to hardcode it.
I don't ever recommend hardcoding a path if you plan to pass this software around. It's never a sure thing...
I need to remove Application launch and "Pin this application to taskbar" from the taskbar context menu for an application. Reason is that the application cannot start standalone, it must be fed with information from another application.
Does anyone know how?
According to this post, you can use the Windows API Code Pack but the required classes are internal. The OP said that they copied 50k lines of code to get it working. I'm not sure if it's improved since that post but here's a workaround I just thought of. Since you can only pin EXE files (and shortcuts as per comment) to the taskbar, you could rename your application to a non-exe extension (most non-exe extensions cannot be pinned).
When you want to call it from your other app, rename it to .exe, launch it, then rename it back again. For example:
Process p = new Process();
//fake extension so it can't be drag/dropped to taskbar
string fakeExtensionName = #"C:\MyFile\myProgram.test";
//what it's actually called
string exeExtensionName = #"C:\MyFile\myProgram.exe";
//rename the fake one to the real one
File.Move(fakeExtensionName, exeExtensionName);
p.StartInfo.FileName = exeExtensionName;
//launch the real one
p.Start();
//rename it back to the fake extension
File.Move(exeExtensionName, fakeExtensionName);
Anyone can rename it to an exe if they really wanted to, so your program should assume that a user can launch it directly and handle that scenario, but any file can be pinned to the taskbar by renaming it to an exe so there's no protection around that.
Ok, i found a ugly but easy solution here https://stackoverflow.com/a/3872503/1323570
apparantly the registry HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\FileAssociation\AddRemoveNames contains some words that may not exist in an executable if pinning should be possible.
you can also read more here: http://www.west-wind.com/weblog/posts/2009/Oct/08/Application-that-wont-Pin-to-Taskbar-in-Windows-7
Edit:
Found the way to do it properly:
Add the key:
HKEY_CLASSES_ROOT\Applications\Example.exe\NoStartPage
ref: http://msdn.microsoft.com/en-us/library/windows/desktop/hh127439(v=vs.85).aspx
I am looking for resources that have examples for creating a Console Application. I am past the "Hello World" stage but am stumped at the point where I need to run an application. I have the string that I need to run that I pulled from the batch file that I am trying to automate in a C# app. I need help with knowing which classes and namespaces have the functionality I need to run it.
Edit: Sorry for the poorly asked question. I'll rewrite it.
I am trying to create a console application that will replace a batch file that I have partially written. Some of the data and file manipulations that I need to do are more complex than can easily be done in a batch file. So far I am reading, writing, and manipulating the files just fine. I am having difficulty when trying figure out how to run a command to execute an application on the server with the proper arguments being passed in.
Update: a coworker gave me the following code snippit which is exactly what I needed to move forward. I am sorry the question was worded so badly.
public static string MyDOSMethod()
{
ProcessStartInfo si = new ProcessStartInfo("cmd.exe");
si.RedirectStandardInput = true;
si.RedirectStandardOutput = true;
si.UseShellExecute = false;
Process p = Process.Start(si);
p.StandardInput.WriteLine(#"cd \windows\system32");
p.StandardInput.WriteLine("exit");
try
{
return p.StandardOutput.ReadToEnd();
}
catch (Exception e)
{
return e.Message;
}
}
The question is not perfectly clear, what I understood is "I need to start an application from my console application; what class can I use?"
My answer is: you should have a look at the static method Process.Start (and in general at the class Process of namespace System.Diagnostics).
Have a look at this tutorial, it will guide you through the usage of Process.Start and ProcessStartInfo (which runs a process and gives you feedback)
I recommend this introduction to C# if your new to programming.
http://www.csharp-station.com/Tutorial.aspx
In order to compile you need a compiler and a GUI to edit code in is also nice :o) Here you can use the free version of Visual Studio:
http://www.microsoft.com/express/
In Visual Studio just click new select console application and i think you will get a "default application" like Hello World that you can run and build.
in my application I've registered a file type (.asm) with my application (it's a tabbed notepad application), and when those files are double-clicked they open with my application through the arguments passed when it's loaded:
static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Main(args));
}
Now the problem is, while this does work, if one instance of my application is already running, whenever a file is opened, a new instance of it is created rather than a new tab being opened in the current instance which I don't want. So I thought of checking if the program is already running, and if it is then I would call a separate function in the main form to load that document instead. But the problem is, I don't know how you call a function in Main.cs from Program.cs, how do we do that?
It's more complicated than just calling a function in Main.cs from Program.cs because the OS will launch a second process for you at the time you double-click your registered files. You need some way to find out if there is another existing process running already and then communicate with that existing process, if there is one.
Fortunately, there is a class in the .NET Framework that already does all of the hard work for you:
Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase
See this blog for a complete example:
http://windowsclient.net/blogs/suryahg/archive/2008/08/20/use-of-microsoft-visualbasic-applicationservices-part-2.aspx
The technique in this question might help:
C# : how to - single instance application that accepts new parameters?
When you double-click on a Word document, Word is automatically run and the document is loaded.
What steps are needed to do the same thing with my C# application?
In other words, assume my application uses ".XYZ" data files. I know how to tell Windows to start my application when a .XYZ data file is double clicked. But how do I find out within my application what data file was chosen so I can load it?
Granted this is a VB.NET solution, but this article has details on how to create the file association for your application in the registry and how to retrieve the command arguments when your application is fired up to do the proper file handling.
It looks easy enough to port over to C#.
The arguments should contain the path to the data file.
You can tweak this behavior and pass additional arguments. Look at the example in this image. Here the file path is passed with %1.
Quicktime File Association Dialog
I think what you are looking for is command-line arguments. If you look at the Open action for .doc for example you'll probably see something like this 'word.exe %1'. Windows will take the name of the file and substitute it in for %1 and then execute the command 'word.exe whatever.doc'. Then from within the application you can see what was passed as an argument into the program see this MSDN article for more details.
I believe it is just a command-line argument that gets passed into your app. Then you can read it with Environment.GetCommandLineArgs. I know that is the case if you drag-and-drop a file onto your app. I have not done what you are describing myself but I assume it works the same way.
I did this in a project I was working on awhile ago and don't have the source code handy, but I believe it really came down to this:
//program.cs
[STAThread]
static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
if (args.Length > 0)
{
//launch same form as normal or different
Application.Run(new Form1(args));
}
else
{
Application.Run(new Form1());
}
}
args is empty when the application is normally started, but when you've properly linked in the association to your .xyz file, when one of those files is selected, your application will be launched with the file location as the first element of the string[]. Certainly either in program.cs or your launch form, I would add validation, but at the base level I believe this is what you need to do.