Close running application on uninstalling - c#

I use ClikOnce for installation my application. But, when application is running and you try to delete it, ClickOnce says that application was deleted, but program is still running.
I know it will be deleted after reboot. But, my program can be autostarted. Therefore, it won't be deleted.
So, how can I force ClickOnce to close the application?

Try this one:
foreach (Process prog in Process.GetProcessesByName("MSACCESS")) {
if (prog.ProcessName == "MSACCESS") {
prog.Kill();
}
}
You need to change "MSACCESS" with your own application proccess name.

Related

Forcing ClickOnce application update on Windows Startup?

I have a ClickOnce application that should automatically check for updates before the application starts. If I start the application manually this also works perfectly.
However I have also added a registry entry to start the application at windows startup and in this case the check for an update is not performed and the application starts just as if there would be no update - I guess because by the time the application starts the connection to the network drive on which the ClickOnce application installation is published is not yet established.
As a workaround I tried to manually force the application in my code by calling this after my MainWindow is already loaded:
private void checkforupdate()
{
try
{
ApplicationDeployment ad = ApplicationDeployment.CurrentDeployment;
if (ad.CheckForUpdate())
{
MessageBox.Show("Update available!");
ad.Update();
}
}
catch { }
}
However for some reason this code still only triggers an update when I start the application manually, when it's started automatically on Windows start nothing happens.
The part of my code where I call checkforupdate is after there was already a few things loaded from the very same drive where the ClickOnce installation files are published so the connection must be established by then.
Does anyone know what to do?
Ok, I found out that I was having a severe misunderstanding of how the update of a ClickOnce application works: In the registry entry which starts the program at Windows start-up I referred to the .exe file in the mysterious Users\AppData\Local\Apps\2.0.... folders - while this does work of course for starting the application itself it does not have anything to do with the update functionality itself.
The update only happens when referring to ClickOnce Application reference (.appref-ms) Shortcut on the Desktop.

Run C# app on Startup?

I have created a simple weather application and I added the code below to let the user let it run on Startup:
RegistryKey rk = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
if (startupCheck.Checked) {
rk.SetValue("WeTile", "\"" + Application.ExecutablePath.ToString() + "\"");
} else {
rk.DeleteValue("WeTile", false);
}
Now this runs fine on both my computers. But when I gave the app to my girlfriend. She said the app does not run on windows start up. I read it online that it could be because of the user permission or the location so I told her to move the app to c:/ and try checking the box again and then restarting. Now it works but on every startup she has the default windows message saying you want to run this app?
How do I get rid of this? What is the best way to add to windows startup that works with both windows 32/64 bit without any user permission disruptions?
It sounds like you may have run afoul of Windows' file blocking security function. Applications created on another computer are automatically blocked from executing unless the user specifically "unblocks" the file. Have your girlfriend right-click on the executable, select "Properties" and see if there is a button at the bottom of the dialog to unblock the file.
Once unblocked, you should no longer see the confirmation prompt at startup.
You could add it to the Windows startup folder, check if it's not there already and if not, add it (assuming this is what the user wants).
See How do I set a program to launch at startup

how can I check and kill another instance of my application?

I face to a problem that my customer is already running the application appA. Then they go to desktop (not kill the appA), and upgrade the application with the version up by run appA.ps1 with PowerShell. After that, click on appA after installed -> get an exception. I guess the root cause is that have another instance is running.
My question is how can I check my application is already running? Can I kill it?
Additional, my application is windows 8 store, c#.
If you wand to keep your current instance running and kill all other instances of your application, you can do this:
using namespace System.Diagnostics;
...
// get current process
Process current = Process.GetCurrentProcess();
// get all the processes with currnent process name
Process[] processes = Process.GetProcessesByName(current.ProcessName);
foreach (Process process in processes)
{
//Ignore the current process
if (process.Id != current.Id)
{
process.Kill();
}
}
From Social MSDN:
All the Metro-style applications work in the highly sandboxed environment and there is no way to directly start an external application.
You cannot access windows app processes. Unfortunately, you aren't able to check for running windows apps.
I didn't find another way so far.
No Metro-style application
At first, you need the processes:
var processes = Process.GetProcessesByName("your application name")
To check whether your program is already running:
var isRunning = processes.Length > 1;
Then you loop through them and close the processes:
foreach (var process in processes)
{
process.CloseMainWindow();
// OR more aggressive:
process.Kill();
}

How to autostart a c sharp application when booting windows?

I have a solution with two project files in it. One executable is a windows form application and the other one is a console application. Both executables perform different tasks, however, both need to be run at the same time (only the windows form has to be started). Therefore I added following code to my windows form application:
RegistryKey rkApp =
Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run",
true);
public Form1()
{
if (rkApp.GetValue("somename") == null)
{
rkApp.SetValue("somename", Application.ExecutablePath.ToString());
}
When I now restart the PC, everything's gone... Any ideas why this problem is turning up? Thank you!
P.S.: I'm a complete beginner, please be nice :)
You need to create a Windows Service Project and install with a service in windows.
If you have a .EXE file, you can add it to Start Up Programms using MSCONFIG from CMD, it's a Non-Coded Solution, but if you want it to attach to register as a service, then create a Windows Service Project.
Use REGEDIT to confirm that the 'somename' was added, also your code has the problem that it only checks 'somename' was previously set, not that the path is correct. If you ran the code in a different path during testing, it would never be updated. Also RegistryKey should be closed after you are finished with it. By rkApp.Close() or by 'using'.
using (RegistryKey rkApp = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true))
{
if (rkApp.GetValue("somename") != Application.ExecutablePath.ToString())
{
rkApp.SetValue("somename", Application.ExecutablePath.ToString());
}
}
You say only the form needs to be launched, does the form run the console? What happens?
Anothing possibility is when running a program by the registry, the 'Current Directory' is 'C:\Windows\System32' and not the application path. If you are using code such as:
File.ReadAllText('SomeFile.txt');
It would try opening C:\Windows\System32\SomeFile.txt and not \YourApp\SomeFile.txt, which can be fixed by:
// Reliably get the .EXE directory, this works for both Form and Console applications:
var AssemblyDirectory = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
// Option 1 is set on load:
Directory.SetCurrentDirectory(AssemblyDirectory);
// Option 2 is use full paths for anything opening files:
File.ReadAllText(AssemblyDirectory + "\\SomeFile.txt");

how can I run an app automatic after restart?

how can I run an app automatic after restart?
(by c# code) I create A new string in 'runOnce' key in registry with the path of the App.
the OS run this APP before it load the OS
my problem is: My APP loads but explorer doesn't load, after I close my APP, explorer loads
I restart the computer in APP, and after restart I want that my APP reopen
When you click restart from your app, make the following modifications to the registry:
Create an entry in HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run registry branch.
Use
Registry.LocalMachine.CreateSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run\\YourAppName");
to create an entry.
And
RegistryKey myKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run\\YourAppName", true);
myKey.SetValue("YourAppName", "AppExecutablePath", RegistryValueKind.String);
to set the run path.
After the system has restarted, your app starts and removes the restart entry by calling this:
Registry.LocalMachine.DeleteSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run\\YourAppName");
It seems like your best bet would be to add your program to RunOnce, instead of Run. That way it will be started after the next reboot, but you won't have to worry about erasing the key afterwards.
HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce
This is a better answer as you should not create a SubKey. Also this will automatically dispose.
string runKey = #"SOFTWARE\Microsoft\Windows\CurrentVersion\Run";
using (RegistryKey key = Registry.LocalMachine.OpenSubKey(runKey, true))
{
key.SetValue("MyProgram", #"C:\MyProgram.exe");
}

Categories