Console Application, detect launched from an exe file or from terminal - c#

I developed an console application, with the dotnet framework.
I want the application to run when called in a terminal session and when executing the .exe file.
Now I got a problem with the design of my application.
When launching the exe, I need to ask if the program should be terminated, so that the user can read the output of my application (otherwise the application and therefore the terminal would close before any human could read what hs been printed).
But exactly this process of asking before the application stops executing is annoying if the application has been launched from the terminal, let's say by running dotnet myapp.dll.
This is because when launching a terminal session the terminal doesn't close after an application has terminated.
So my question is if there exists any advice/ best practice/ or if there is a way of detecting if the application has been launched via an exe file or in the terminal.
Thanks for your advice.

Related

Windows Application Packaging project with Full Trust Process stays running after closing UWP app

I'm using the Desktop Bridge to create a Full-Trust UWP app so I can run cmd commands from my UWP app. I followed this tutorial and got everything working just fine. However I noticed that when closing the UWP or the trusted process (WinForms app in my case) or both the packaged project keeps running. The debug mode keeps running and I can see the process in my task manager. Is there something extra I have to do to avoid that?
Okay so it wasn't actually an issue. The app will stay running for 30 to 60 seconds then exit. I guess it's to save memory in case it gets launched right after closing. Don't quote me on that though.

WIX Installer do not run C# process properly

We have a big WIX Installer. After the installation, there are around 2800 scripts that should be run to upgrade the database. For that purpose, a special application is created. It just goes to the SQL machine, installs prerequisites like SQL SMO and SQL Sys Clr Types, and executes scripts. This small application is run in custom actions in WIX. Logically, it should be run with elevated privileges. In this custom action, I am using c# Processes to start the app. However, I have tried everything, and the application is still not working properly. It opens the app and app tries to install prerequisites and just exits - nothing is installed. What I have tried:
1. Running process with elevated privileges (process.StartInfo.UseShellExecute = true)
2. Running process with user privileges (standardinput/output with UseShellExecute=false)
3. Running the app with a batch file.
Overall process should be as following:
Installer is run with elevated privileges -> After installation, special application is run to upgrade the database -> the app installs prerequisites -> the app executes scripts.
The interesting part is that application is working properly when I do it manually with cmd. However, when installer's custom action opens cmd.exe (c# Process) and it is still waiting, then manually opening application via cmd is not working. In other words, I have cmd (with elevated rights) opened by installer and another cmd opened by myself manually. Then, manual running the application is not working. As soon as I close the cmd opened by the installer, and run manually the application via cmd that was opened by myself, then again everything is fine, everything is working.
I need a help how can I fix this?
P.S. The installer's prerequisites cannot be extracted and installed separately. We do not have source code of upgrade database application.
After some research, I have found that Windows cannot run two MSI at the same time and therefore, second MSI was silently quitting.

Run a published console app from win form app

I have a win form application that accept exe file and folder path as argument.
Then a console app is launched. But in my case these console apps are
also published.
When I execute from Debug console app work but when I select
Setup.exe from published app the application installs and then
crash. With argument out of range error.
A related answer can be seen here. But it work with unpublished
exe which I am already doing.
Can I get name of the installed console app something like
"WINWORD.exe" or "Chrome.exe".

Attach to running program with powershell

I have a Raspberry Pi v2 running Windows 10 IoT and am trying to run a dotnet 2.0 console application on it.
I have a powershell script start it on startup, but then I do not get the console output from it. I was wondering if there was a way to attach to the running process with powershell on my computer, like you would to a docker container, to get the console output.
I do not want to run my program on my computer, and the Raspberry Pi is the only computer I want to leave on overnight to run the program.
If you can't or don't want to re-start your Application
You are rather limited in this case, since, as stated in another SE Answer by Harry Johnston:
Windows does not provide any mechanism to retroactively give another
process a standard output handle if it was created without one.
(Besides, in most cases an application will check the standard output
handle only during startup.)
If you need to read something that already has been written to the Console, you are probably out of luck.
You should however be able to get any future output by attaching to the process with debugging tools.
In case of the Visual Studio Remote Debugger, this would even allow you to debug the execution of your Console Application, provided you wrote it.
An accepted answer on Superuser.com suggests that StraceNT is also capable of read the output of a running Console Application. This will probably be your best bet if the running Application wasn't written by yourself.
If the Application can be stopped and restarted
Powershell Remoting is probably the best way.
You would need to enable Powershell Remoting on your Raspberry Pi, and then you can use enter-pssession [RaspberryPi-Hostname-or-IP] in your local Powershell console to connect to your Raspberry Pi.
At that point you can just start your console application as if you were running it on your local machine and get all the console output you want.
No need to attach to anything.
You can redirect the output of your console app to a txt file in the current directory. If you run the console app from powershell you can do it like this:
.\[YOURAPP].exe | tee test.txt
And you can access the test.txt from Windows file sharing.

Launch a WPF application from a batch file or console application

I've made a WPF application based on .NET framework 4.
This application will be copied to a pendrive, and the client will have to run it from there.
In order to check if the client has Framework 4 installed, I've made a batch file that read the output of "clrver.exe", and if the framework 4 is installed it runs the application using the command
START "application path"
The problem is that few seconds after the WPF application is running it crashes.
So I've tried to make a C# Console application, just to check, and using Process.Start() I've started the WPF application. The behaviour is exactly the same.
If I try to run the WPF application by clicking directly on the executable it runs perfectly.
If I try to run it from a console, it crashes after few seconds (5~6 seconds).
Does anyone know the reason or an idea on how to run it?
The problem was dued by the working directory that cause some problem when the application is launched by a console application or a batch file.
I've solved replacing all relative paths in my code with absolute paths retrieved at runtime with: System.AppDomain.CurrentDomain.BaseDirectory

Categories

Resources