Adding program to startup - c#

I have created a program which I built to an exe as the release build. When you double click the exe the program should add itself to the startup so everytime the pc is booted, the program runs. It however does not add it to the startup despite getting no errors. The program also runs as expected.
This is the code which handles adding the program to the startup:
public static void AddApplicationToStartup()
{
using(RegistryKey key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true))
{
key.SetValue("WindowsProcesses", "\"" + Application.ExecutablePath + "\"");
}
}
...this code is written inside the main 'Program' class.

Add application path this way
public static void AddApplicationToStartup()
{
using (RegistryKey key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true))
{
key.SetValue("WindowsProcesses", Application.ExecutablePath.ToString());
}
}

Related

FolderBrowserDialog won't show in a single .cs file without Form

I am trying to code a program which is executed when a file is right clicked in windows, and then a context menu feature named 'Move to' executes a file in the windows registry HKEY ClASSES. It ought to parse in "%1" as argument when it executes, so that my program knows where the file is located. However, when I compile my single .cs file, the FolderBrowserDialog won't show. I am suspecting that it is because I haven't initialized some kind of form before I call it. Is it possible in some way to choose a folder from a single c# file without including Forms?
using System;
using System.IO;
using System.Reflection;
using System.Windows.Forms;
public class MoveTo : Form
{
public static string current_file_path;
public static string new_file_path;
public static string file_name;
public static void Main(string[] args){
if (args.Length > 0)
{
current_file_path = (string) args[0];
file_name = (string) current_file_path.Replace(Path.GetDirectoryName(Environment.GetCommandLineArgs()[1]), "");
var browser = new FolderBrowserDialog();
if (browser.ShowDialog()==DialogResult.OK)
{
new_file_path = browser.SelectedPath + file_name;
}else
{
Environment.Exit(1);
}
try
{
File.Move(current_file_path, new_file_path);
}catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
}
If you bypass the argument check and try to show the FBD in a debugger, with this exact code, you will see System.Threading.ThreadStateException: 'Current thread must be set to single thread apartment (STA) mode before OLE calls can be made. Ensure that your Main function has STAThreadAttribute marked on it. This exception is only raised if a debugger is attached to the process.'
As per the error message, this exception won't be raised if no debugger is attached. Put an [STAThread] attribute on your Main method, like you normally see in any windows forms app:
[STAThread]
public static void Main(string[] args)
{
...
I also recommend you add an else for your outer if, to show an error if no arguments are passed (otherwise your app will exit silently

Script Error when open .chm disappear when opening through the debugger

I get a number of script errors and none of the images will be shown when I open a .chm file on my computer. If I esc all error messages and refresh (twice) then the .chm is shown correctly. Although I need to do this for each new page.
I have made all recommended fixes for .chm files! Reregistrered, unblocked, fixed paths,... The errors is for all .chm on the machine
But, here is my real question, if I run this program, with a .chm file as argument, through the Visual Studio 2013 debugger then the .chm is shown correctly!
The problem is probably in my Windows configuration, but somehow the debugger "fixes" this error and get it to work. Does the debugger have it's own configuration that isn't dependent on the actual Windows configuration?
using System.Diagnostics;
namespace xcute
{
class Program
{
static void Main(string[] args)
{
if (args.Length > 0)
{
string f = args[0];
Process.Start(f);
}
}
}
}
EDIT: Here are the error dialogs
I have found the problem (well sort of)!
If I open the .chm as administrator then everything works! So obviously I have some permission error on my computer. The reason it worked when I ran my program in the debugger is that Visual Studio is started as Administrator...
But since I'm a programmer I solved the issue by creating a small program that start hh.exe as admin. I get the UAC consent form but I can live with that.
// Anders
The program:
internal class Program
{
private static void Main(string[] args)
{
if (args.Length > 0)
{
Execute(args[0]);
}
}
private static void Execute(string chmFile)
{
const int ERROR_CANCELLED = 1223; //The operation was canceled by the user.
ProcessStartInfo info = new ProcessStartInfo(#"C:\Windows\hh.exe");
info.Arguments = chmFile;
info.UseShellExecute = true;
info.Verb = "runas";
try
{
Process.Start(info);
}
catch (Win32Exception ex)
{
if (ex.NativeErrorCode == ERROR_CANCELLED)
Console.WriteLine("Why you no select Yes?");
else
throw;
}
}
}

How to get the verified publisher of a signed program?

In C#, how do I get the verified publisher of a signed .exe file? I am working on a program blocking application, and would like to be able to detect the publisher, if any, of a program.
I think you can use
X509Certificate.CreateFromSignedFile()
If you pass name of the .exe or .msi or any signed file to this method, it will create a X509Certificate object. You can then use GetName() method to get certification publisher information. Below code should get you started if you have not discovered it already.
using System;
using System.Security.Cryptography.X509Certificates;
namespace ConsoleApplication1
{
public class ConsoleApplication1
{
[STAThread]
static void Main(string[] args)
{
X509Certificate xcert = null;
try
{
xcert = X509Certificate.CreateFromSignedFile(args[0]);
Console.WriteLine(args[0] + "\t" + xcert.GetName() + "\t" + xcert.GetPublicKeyString());
}
catch (Exception e) { Console.WriteLine(args[0] + ": Unable to readDER-encoded signature."); }
}
}
}

After updating EnvironmentVariable - PATH - I still get a FileNotFoundException on Windows 2003 Server

I'm just starting with a new product and I guess I don't understand the PATH variable. My documentation says to update the PATH like this which I do successfully in a little console application:
using HP.HPTRIM.SDK;
namespace TestSDKforTRIM71
{
class Program
{
static void Main(string[] args)
{
string trimInstallDir = #"C:\Program Files\Hewlett-Packard\HP TRIM";
string temp = Environment.GetEnvironmentVariable("PATH") + ";" + trimInstallDir;
Environment.SetEnvironmentVariable("PATH", temp);
DoTrimStuff();
}
public static void DoTrimStuff()
{
using (Database db = new Database())
{
db.Connect();
Console.WriteLine(db.Id);
}
Console.ReadKey();
}
}
}
In the above project, I have a reference to HP.HPTRIM.SDK which exists at:
C:\Program Files\Hewlett-Packard\HP TRIM\HP.HPTRIM.SDK.dll
After the above ran successfully, I tried to permanently change the PATH by using Control Panel:System:Advanced:Environment Variables. I verified the above PATH by examining the registry at HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment. I see the following as the last entry in the PATH value:
;C:\Program Files\Hewlett-Packard\HP TRIM\
I thought this would permanently SET this at the end of the PATH but when I run the above console program with a few lines commented out I get the FileNotFoundException (see below). I am confused about how to get this in the PATH and not have to worry about it anymore.
using HP.HPTRIM.SDK;
namespace TestSDKforTRIM71
{
class Program
{
static void Main(string[] args)
{
//string trimInstallDir = #"C:\Program Files\Hewlett-Packard\HP TRIM";
//string temp = Environment.GetEnvironmentVariable("PATH") + ";" + trimInstallDir;
//Environment.SetEnvironmentVariable("PATH", temp);
DoTrimStuff(); // without setting the PATH this fails despite being in REGISTRY...
}
public static void DoTrimStuff()
{
using (Database db = new Database())
{
db.Connect();
Console.WriteLine(db.Id);
}
Console.ReadKey();
}
}
}
Only newly started processes that don't inherit their environment from their parent will have the updated PATH. You'll have to at least restart the Visual Studio hosting process, close and re-open your solution. To cover all possible corners, log out and log back in so that Windows Explorer (and thus Visual Studio) also start using the updated environment.

How to run a C# application at Windows startup?

I made an application that launches during startup, with the next code below.
The process runs on the process manager tool after the restart, but I can't see
the application on the screen.
When I open the same .exe file from the startup registry value the program runs perfect.
// The path to the key where Windows looks for startup applications
RegistryKey rkApp = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
// Add the value in the registry so that the application runs at startup
rkApp.SetValue("MyApp", Application.ExecutablePath.ToString());
What can I do to fix it up?
Code is here (Win form app):
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;
namespace RunAtStartup
{
public partial class frmStartup : Form
{
// The path to the key where Windows looks for startup applications
RegistryKey rkApp = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
public frmStartup()
{
InitializeComponent();
// Check to see the current state (running at startup or not)
if (rkApp.GetValue("MyApp") == null)
{
// The value doesn't exist, the application is not set to run at startup
chkRun.Checked = false;
}
else
{
// The value exists, the application is set to run at startup
chkRun.Checked = true;
}
}
private void btnOk_Click(object sender, EventArgs e)
{
if (chkRun.Checked)
{
// Add the value in the registry so that the application runs at startup
rkApp.SetValue("MyApp", Application.ExecutablePath);
}
else
{
// Remove the value from the registry so that the application doesn't start
rkApp.DeleteValue("MyApp", false);
}
}
}
}
Try this code:
private void RegisterInStartup(bool isChecked)
{
RegistryKey registryKey = Registry.CurrentUser.OpenSubKey
("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
if (isChecked)
{
registryKey.SetValue("ApplicationName", Application.ExecutablePath);
}
else
{
registryKey.DeleteValue("ApplicationName");
}
}
Source (dead): http://www.dotnetthoughts.net/2010/09/26/run-the-application-at-windows-startup/
Archived link: https://web.archive.org/web/20110104113608/http://www.dotnetthoughts.net/2010/09/26/run-the-application-at-windows-startup/
You could try copying a shortcut to your application into the startup folder instead of adding things to the registry. You can get the path with Environment.SpecialFolder.Startup. This is available in all .net frameworks since 1.1.
Alternatively, maybe this site will be helpful to you, it lists a lot of the different ways you can get an application to auto-start.
public class StartUpManager
{
public static void AddApplicationToCurrentUserStartup()
{
using (RegistryKey key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true))
{
key.SetValue("My ApplicationStartUpDemo", "\"" + System.Reflection.Assembly.GetExecutingAssembly().Location + "\"");
}
}
public static void AddApplicationToAllUserStartup()
{
using (RegistryKey key = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true))
{
key.SetValue("My ApplicationStartUpDemo", "\"" + System.Reflection.Assembly.GetExecutingAssembly().Location + "\"");
}
}
public static void RemoveApplicationFromCurrentUserStartup()
{
using (RegistryKey key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true))
{
key.DeleteValue("My ApplicationStartUpDemo", false);
}
}
public static void RemoveApplicationFromAllUserStartup()
{
using (RegistryKey key = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true))
{
key.DeleteValue("My ApplicationStartUpDemo", false);
}
}
public static bool IsUserAdministrator()
{
//bool value to hold our return value
bool isAdmin;
try
{
//get the currently logged in user
WindowsIdentity user = WindowsIdentity.GetCurrent();
WindowsPrincipal principal = new WindowsPrincipal(user);
isAdmin = principal.IsInRole(WindowsBuiltInRole.Administrator);
}
catch (UnauthorizedAccessException ex)
{
isAdmin = false;
}
catch (Exception ex)
{
isAdmin = false;
}
return isAdmin;
}
}
you can check whole article here
its very simple
add two part in code :
1- add namespace:
using Microsoft.Win32;
2-add application to registery :
RegistryKey key=Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
key.SetValue("your_app_name", Application.ExecutablePath);
if you want delete app from registery:
key.DeleteValue("your_app_name",false);
first I tried the code below and it was not working
RegistryKey rkApp = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
rkApp.SetValue("MyAPP", Application.ExecutablePath.ToString());
Then, I changed CurrentUser with LocalMachine and it works
RegistryKey rkApp = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
rkApp.SetValue("MyAPP", Application.ExecutablePath.ToString());
I did not find any of the above code worked. Maybe that's because my app is running .NET 3.5. I don't know. The following code worked perfectly for me. I got this from a senior level .NET app developer on my team.
Write(Microsoft.Win32.Registry.LocalMachine, #"SOFTWARE\Microsoft\Windows\CurrentVersion\Run\", "WordWatcher", "\"" + Application.ExecutablePath.ToString() + "\"");
public bool Write(RegistryKey baseKey, string keyPath, string KeyName, object Value)
{
try
{
// Setting
RegistryKey rk = baseKey;
// I have to use CreateSubKey
// (create or open it if already exits),
// 'cause OpenSubKey open a subKey as read-only
RegistryKey sk1 = rk.CreateSubKey(keyPath);
// Save the value
sk1.SetValue(KeyName.ToUpper(), Value);
return true;
}
catch (Exception e)
{
// an error!
MessageBox.Show(e.Message, "Writing registry " + KeyName.ToUpper());
return false;
}
}
An open source application called "Startup Creator" configures Windows Startup by creating a script while giving an easy to use interface. Utilizing powerful VBScript, it allows applications or services to start up at timed delay intervals, always in the same order. These scripts are automatically placed in your startup folder, and can be opened back up to allow modifications in the future.
http://startupcreator.codeplex.com/
for WPF: (where lblInfo is a label, chkRun is a checkBox)
this.Topmost is just to keep my app on the top of other windows, you will also need to add a using statement " using Microsoft.Win32; ", StartupWithWindows is my application's name
public partial class MainWindow : Window
{
// The path to the key where Windows looks for startup applications
RegistryKey rkApp = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
public MainWindow()
{
InitializeComponent();
if (this.IsFocused)
{
this.Topmost = true;
}
else
{
this.Topmost = false;
}
// Check to see the current state (running at startup or not)
if (rkApp.GetValue("StartupWithWindows") == null)
{
// The value doesn't exist, the application is not set to run at startup, Check box
chkRun.IsChecked = false;
lblInfo.Content = "The application doesn't run at startup";
}
else
{
// The value exists, the application is set to run at startup
chkRun.IsChecked = true;
lblInfo.Content = "The application runs at startup";
}
//Run at startup
//rkApp.SetValue("StartupWithWindows",System.Reflection.Assembly.GetExecutingAssembly().Location);
// Remove the value from the registry so that the application doesn't start
//rkApp.DeleteValue("StartupWithWindows", false);
}
private void btnConfirm_Click(object sender, RoutedEventArgs e)
{
if ((bool)chkRun.IsChecked)
{
// Add the value in the registry so that the application runs at startup
rkApp.SetValue("StartupWithWindows", System.Reflection.Assembly.GetExecutingAssembly().Location);
lblInfo.Content = "The application will run at startup";
}
else
{
// Remove the value from the registry so that the application doesn't start
rkApp.DeleteValue("StartupWithWindows", false);
lblInfo.Content = "The application will not run at startup";
}
}
}
If you could not set your application autostart you can try to paste this code to manifest
<requestedExecutionLevel level="asInvoker" uiAccess="false" />
or delete manifest I had found it in my application
OK here are my 2 cents: try passing path with each backslash as double backslash. I have found sometimes calling WIN API requires that.
I think there is a specific Win32 API call which takes the application path and puts it in the registry automatically for you in the proper location, I've used it in the past but I don't remember the function name anymore.

Categories

Resources