C# How to put another exe in Windows startup from code - c#

I never needed to do this before and the examples i saw only show me how to put my current app from code in startup but not other .exe file in startup using code from current app

you can startup anything you want via using c# code using Process class like...
private void btnOpenApp_Click(object sender, EventArgs e)
{
System.Diagnostics.Procces.Start("appName.exe");
}

Related

Is it possible for me to open a native application (Example Microsoft Whiteboard) of windows in my c# windows form application?

Below is an example of how I open Zoom and Teams
private void button2_Click_1(object sender, EventArgs e)
{
string user = System.Windows.Forms.SystemInformation.UserName.ToString();
System.Diagnostics.Process.Start("C:/Users/" + user + "/AppData/Local/Microsoft/Teams/current/Teams.exe");
}
private void button1_Click_1(object sender, EventArgs e)
{
string user = System.Windows.Forms.SystemInformation.UserName.ToString();
System.Diagnostics.Process.Start("C:/Users/" + user + "/AppData/Roaming/Zoom/bin/Zoom.exe");
}
Note: Unlike Zoom and Teams that when installed they create a folder where we find all the installation supplements, but Whiteboard does not have any folder against these supplements. That's why it's very difficult to open it from my application in C#
Microsoft Whiteboard is a store app.
Here's a Post about How to open Microsoft Store apps from Command Prompt?
You can find these apps here:
shell:AppsFolder
From CMD you can start it with
explorer.exe shell:appsFolder\Microsoft.Whiteboard_8wekyb3d8bbwe!Whiteboard
From code it can be started this way:
System.Diagnostics.Process.Start(#"C:\Windows\explorer.exe shell:appsFolder\Microsoft.Whiteboard_8wekyb3d8bbwe!Whiteboard");
Another example can be found here:
https://notepad.onghu.com/2020/launch-win10-app-from-cmdline/
From the code it seems like you just want to start the process, not run it directly on your winform app. By default, applications downloaded from Microsoft Store are stored in C:/Program Files/WindowsApps.

C# Loading an exe on a file share

I am trying to run a program on a Citrix VM located at \\site.local\shares\Agent_console\AC_Launch\AC_Launcher.exe /env=PROD
I am currently using the code below and it is stating the following
private void button13Chromatix_Launcher_Click(object sender, EventArgs e)
{
Process.Start(#"\\plumbedford.local\shares\Agent_Console\AC_Launch\AC_Launcher.exe",
"/Env=PROD");
}
But that fails with the following error:
Unable to access Agent Console XML network share. Try again?
This is a desktop shortcut that works using the same directory.
Thank you in advance.
Try using a literal string (with an # prefix), to ensure that backslashes are not misinterpreted.
It also looks like you have some typos in the location.
Try this:
Process.Start(#"\\site.local\shares\Agent_Console\AC_Launch\AC_Launcher.exe", "/env=PROD");

Run ASP.NET project inside a Windows Forms Application?

We have a very simple asp.net project that's composed of the following: Home.aspx, web.config, and a few images that Home.aspx uses. This webform also connects to a sql server database.
So let's say a client purchases our website app. So we go to their offices and we first install the SQL Server database in a server within their network.
Now, this is where I'm stuck: once we create the database (and configure web.config), we want to hand our client a Windows Application (ie. an EXE) that, when double-clicked, will open a Windows Form with a browser control that will display [Home.aspx].
The issue is that I want to include everything in this Windows executable. All images, [Home.aspx], web.config will be included in this executable. The only reason [Home.aspx] would go beyond this Windows Executable is to connect to the sql server database that's in the network. It's as if the Windows Application has everything needed to host this website.
Is this possible?
Thanks.
Dude the Only way of doing this is either making your ASP.NET Web application a web API on a host server or making it an API on Local Host. So if you are trying to do it on client's Machine you will be needing IIS Installed over that machine. Here is a Complete Guide that how you can Run ASP.NET Web API in your Windows form application. So what you have to do is
1: Make your ASP.NET Web API
2: Make your Windows Form Application
3: Call that API from your Windows Form Application
4: Enjoy
Cheers
It is very likely that you need to ship a web server with your WinForms app, such as
http://ultidev.com/products/UWS-Cassini-Pro/Default.aspx
https://cassinidev.codeplex.com/
C# program that shows WebBrowser event handlers
You can also set the Url property to change the current page. As with other controls, the WebBrowser offers event handlers. These trigger when a page is being loaded and when the page is loaded. Here
using System;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// When the form loads, open this web page.
webBrowser1.Navigate(" http://localhost/Home.aspx");
}
private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
// Set text while the page has not yet loaded.
this.Text = "Navigating";
}
private void webBrowser1_DocumentCompleted(object sender,
WebBrowserDocumentCompletedEventArgs e)
{
// Better use the e parameter to get the url.
// ... This makes the method more generic and reusable.
this.Text = e.Url.ToString() + " loaded";
}
}
}

Hangfire shows two instances running on the same server v.1.5.3 - Leads to errors

I have followed the documentation specified here to make the application always running and enable Service Auto-start. For configuration I used the documentation specified here and in this application I'm using Hangfire version 1.5.3. I have two other Hangfire applications running on the same server, one is using Hangfire v.1.4.1 and the other is using 1.4.5. Both of these work flawlessly. Every application runs under it's own application pool and there is no difference to the code.
The application that does not work adds a GUID after the port number as you can see in the pictures below. This application some times does not auto start and I think it has to do with the two server instances.
I know that Hangfire has modified something with ServerName because if you use "BackgroundJobServerOptions" and "ServerName" variable you get the obsolete message:
"Server Id is auto-generated now, and this option does not make sense
anymore. Will be removed in 2.0.0."
Has anyone experienced this and managed to solve it?
Note: I'm not using BackgroundJobServerOptions in any of the applications and I have tried to reboot the server.
The two applications that work:
Found the problem. It seems that in version 1.5.3, probably 1.5 <, you should not use app.UseHangfireServer() in your OWIN Startup class if you have registered the application start in Global.asax as well. When I commented out the code like below everything started working again.
public partial class Startup
{
public void Configuration(IAppBuilder app)
{
ConfigureAuth(app);
//GlobalConfiguration.Configuration.UseSqlServerStorage("DefaultConnection");
//app.UseHangfireServer();
}
}
Update:
I was following this tutorial:
http://docs.hangfire.io/en/latest/deployment-to-production/making-aspnet-app-always-running.html
Global.asax.cs:
protected void Application_Start(object sender, EventArgs e)
{
HangfireBootstrapper.Instance.Start();
}
protected void Application_End(object sender, EventArgs e)
{
HangfireBootstrapper.Instance.Stop();
}

Why does using Process.Start makes the started app run from the parent app location instead of the started app's actual location?

I've run into a problem where using Process.Start on an exe file for another application would cause that application to warn about missing files and then crash. After a bit of testing and debugging it looks like, somehow, when using Process.Start, it makes the targeted exe run from the location of the parent program that launched it, rather than from the location where that exe is actually located.
Why does this happen? Is this intentional design? Needless to say this completely messes up the targeted app that you run, so how should this method of launching another exe be used?
Here's an example to demonstrate the problem:
Let's create a simple small app that searches for a file and prints "success" if it finds it, and "fail" if it doesn't.
using System;
using System.IO;
using System.Windows.Forms;
namespace Testapp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
if (File.Exists(#".\test\test.txt"))
{
MessageBox.Show("Program executed successfully");
}
else
{
MessageBox.Show("Program failed to execute properly");
}
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
}
The intended folder structure of the above program looks like this:
Testapp Folder
->Testapp.exe
->Test Folder
->test.txt
The program goes into the Test folder, if it exists, and searches for the test.txt file.
Now let's make a quick launcher app that will be used to run our test app:
using System;
using System.Diagnostics;
using System.Windows.Forms;
namespace LaunchTestApp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
Process.Start(#".\Testapp.exe");
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
}
The above application is intended to reside in the same directory as our Testapp.exe. When this is run that way, everything works out perfectly fine. The Launchapp runs our Testapp, and we can click on the button in our Testapp and see a messagebox that the program had executed successfully.
However, now let's make a slight change to our launching application. Our launching application will now reside on one directory level above our Testapp.exe, like so:
Launching App Folder
->Launchapp.exe
->Testapp Folder
->Testapp.exe
->Test Folder
->Test.txt
Naturally, we also change our Launchapp to target our Testapp.exe in a different directory, like so:
Process.Start(#".\Testapp\Testapp.exe");
Now when the Launchapp is run, and it consequently runs Testapp.exe, and we press our button, our Testapp.exe displays a message that the program failed to run properly.
At this point, if we go back to our Testapp code and change the location to search for the test.txt from .\test\test.txt\ to .\Testapp\test\test.txt, then the program will run successfully.
This demonstrates that the Testapp exe seems to be running from the location of Launchapp, rather than from the location of where Testapp exe is actually located.
Why does this happen, and how can I prevent it from happening? I want to be able to run an application located in a different folder from the one where my calling application resides, and thus it is obviously important that the started process runs from the location where that process is located, and not where the calling application is located.
Processes inherit the working folder of the process that started them by default. It's just the way windows works (yes, it is intentionally done this way, other operating systems behave the same way).
You can use the .WorkingDirectory property of a ProcessStartInfo to start it from another path. http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo(v=vs.110).aspx
In the absence of any other explicit instructions, it will always use the parent process's environment. This is just how it works. If you want to specify the working location on the filesystem for the new process, use the overload that accepts a ProcessStartInfo instance. This allows you to set the working folder along with many other values.
You're misusing the term "run from".
When you run any program, its current directory is the current directory of its parent process, unless specified otherwise.
Usually (from explorer or a command prompt), this is the directory containing the EXE file.
If you start it from your own program, it will be the current directory of your program.
You can explicitly pass an arbitrary current directory in StartOptions.
Use like this:
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.UseShellExecute = false;
startInfo.WorkingDirectory = #"THE DIRECTORY";
Process.start(startInfo);

Categories

Resources