Registry Startup not working windows application - c#

I created windows application ,I want to start my application on windows startup
for that i written following code in installer class. but when i am checking registry using regedit i didnt get registry value. and my application not working.
public override void Commit(IDictionary savedState)
{
base.Commit(savedState);
try
{
RegistryKey add = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
add.SetValue("ToposcreenServer", "\"" + Application.ExecutablePath.ToString() + "\"");
RegistryKey key = Registry.LocalMachine.CreateSubKey(#"SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\{70E25B31-99A9-474C-8990-CE28FBCEAAD1}", RegistryKeyPermissionCheck.Default);
if (key != null)
{
key.SetValue("SystemComponent", 1, RegistryValueKind.DWord);
key.Close();
}
Directory.SetCurrentDirectory(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location));
Process.Start(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\ToposcreenServer.exe");
GLobalclass.WriteLog("Installer Executed");
}
catch (Exception ex)
{
GLobalclass.WriteLog("Installer Error :" + ex.Message);
}
}

If this is an Everyone install then that code won't write to HKCU of the installing user because the code is running with the System account credentials, not the installing user's.
Anyway, you don't need code to set the Run key. Go to the registry view in the IDE and add registry folders to get to that Run key in HKCU. Then add an item with the Nama ToposcreenSaver and the value [TARGETDIR]my.exe assuming your executable is in the Application Folder in the File System view. It's possible that it won't run anyway if it requires elevation on a UAC system.
(If this code is really in an installer class, it's also not clear why you're using Application and ExecuteablePath because an installer class is a Dll being called from an msiexec.exe process, and is nothing at all to do with whatever executable you want to run. Surely it's the name of an executable you are installing?)
You don't need to set SystemComponent in the registry key. That registry key may not be there at the time your custom action runs, and what you should really do is open your MSI file with Orca and add ARPSYSTEMCOMPONENT to the Property table, give it a value of 1.
https://msdn.microsoft.com/en-us/library/windows/desktop/aa367750(v=vs.85).aspx
If the app really is a conventional screensave this might be the best way to do it:
http://www.advancedinstaller.com/user-guide/qa-install-screensaver.html

You need to check that add is not null, as per https://msdn.microsoft.com/en-us/library/xthy8s8d(v=vs.110).aspx .

Related

Custom Uninstall not deleting file in .net Application

I have a settings file created when user run the wpf application.
I have created a custom uninstaller to delete some registry keys related to my app and to delete this setting file.
But my file is not getting deleted.
Here is the code -
public override void Uninstall(IDictionary savedState)
{
base.Uninstall(savedState);
try
{
using (RegistryKey registryKey = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true))
{
if (registryKey.GetValue("TeachingManagementTool") != null)
{
registryKey.DeleteValue("TeachingManagementTool", true);
}
}
if (File.Exists("Setting.ini"))
File.Delete("Setting.ini");
}
catch (Exception ex)
{
MessageBox.Show("Registry Keys exception " + ex.Message);
}
}
I tried Directory.GetCurrentDirectory() to get file names and delte it, but it doesnt work. So I checked this line of code works file.Delete(filename). It delets the specified file. So it should delete the file during uninstall as its in the same folder.
At the end I should say- I tried 2-3 different ways to access that file and delete it during uninstallation. but Its not delteting and throwing error some times and sometimes no exception at all.
The exception was related to Access to SysWOW64\AdvanceInstaller is
denied
FYI - MY App has <requestedExecutionLevel level="highestAvailable" uiAccess="false" /> already.
I tried solutions available on StackOverflow but its not working So I needed to ask a new question. So please let me know where I am mistaking. I am sure it is something very minor that I might be missing here
Advanced Installer: So you are using Advanced Installer? In the Files and Folders view, select the destination folder in question. In the right pane, right click inside the folder where the file to remove resides. Do you see "New File Operation"? Select "File Removal" and configure options.
Remember to set the options properly. In particular "Remove On". Select on "Component Uninstall".

How can I set this registry value for my User from my installer

The problem from
https://stackoverflow.com/a/37859812/4878558
I need to set Registry value for current user, who launch the install up. Since install going for system mode - I don't know anything about current user
Also my code giving 'System.UnauthorizedAccessException'
SecurityIdentifier sID = WindowsIdentity.GetCurrent().User;
var subKey = Registry.Users.OpenSubKey(sID + "\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");
subKey.SetValue("test", "test");
enter code here
As Ripple and I have both commented, there's no need for code. Go to the Registry view in the setup project, right-click on Software under HKEY_CURRENT_USER and add the key Microsoft, then Windows, the CurrentVersion, then Run, adding each key.
Then in the Run key view, right-click in the Name, View pane on the right and add new string value, the name being your name. The value, I assume, is the path to your exe, and (assuming it's in the Application folder) make the value [TARGETDIR]my.exe.
If your install is an "Everyone" install then there is a perfectly good reason why it cannot work. This is nothing to do with the code. In an Everyone install that custom action code is running with the System account (NOT the installing user) so you are trying to create a run key for the system account.
Here is how to write autostartup options:
const string AutorunRegistryKey = #"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run";
Registry.SetValue(AutorunRegistryKey, <AppName>, <PathToApplication>);
If you want to remove it from autostartup:
const string AutorunRelativePath = #"Software\Microsoft\Windows\CurrentVersion\Run\";
var key = Registry.CurrentUser.OpenSubKey(AutorunRelativePath, true);
if (key != null)
{
key.DeleteValue(<AppName>, false);
key.Close();
}

How can I get my app to run from the Start menu?

If you type "regedit" in the Start menu's edit box and mash the Enter key, Registry Editor will be invoked. The same is true for "cmd" and the Command Line, and doubtless several other apps.
How can I get my app to respond the same way, so that if the user enters "Platypus" in the Start menu edit box, Platypus.exe will be invoked?
Does it require manipulation of the Registry / adding an entry somewhere there, and if so, just what key and value needs to be added?
I would be satisfied with the user needing to run the app manually once (2-clicking its icon; it's a Winforms app), at which time startup code (no pun intended) would do whatever was necessary to make the app henceforth Startsmartable (Windows key, "Platypus", to start the app).
I know that it's just as easy/easier for the user to simply 2-click a desktop icon when they want to run the app, but this particular functionality is not my idea, so complaints about the oddity of this question would be to no avail.
UPDATE
I added the code recommended by Chandan (with my executable's name):
public static void AddToStartup()
{
using (RegistryKey startup = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true))
{
startup.SetValue("RoboReporter", "\"" + System.Windows.Forms.Application.ExecutablePath + "\"");
}
}
...called it from the main form's load event:
private void FormRoboReporter_Load(object sender, EventArgs e)
{
RoboReporterConstsAndUtils.AddToStartup();
}
...shut down the app, went to the Start menu and entered the program's name ("RoboReporter"), and all it did was bring up search results of related file names.
UPDATE 2
What it does do is cause my app to run whenever the computer is restarted. That's not what I want. The code above adds an entry to HKEY_CURRENT_USER.Software.Microsoft.Windows.CurrentVersion.Run as can be seen here (along with a couple of other entries that predated it):
I don't want the app to start up every time the computer restarts, so I removed the entry. The question remains: how can I make the app runnable from the Start menu?
You can add your application's parent directory's path to the environment variable called PATH.
string pathvar = Environment.GetEnvironmentVariable("PATH", EnvironmentVariableTarget.Machine);
Environment.SetEnvironmentVariable("PATH", pathvar + ";" + Application.StartupPath + "\\", EnvironmentVariableTarget.Machine);
(Note that the paths added to this variable should end with a backslash \, and each path is separated by a semicolon ;)
Adding the parent directory's path to the environment variable will make all it's contents quickly accessible from the Start Menu's search field, from Run and from CMD.
You can also change EnvironmentVariableTarget.Machine to EnvironmentVariableTarget.User to modify the variable for the current user only.
EDIT:
A note: Setting a variable for the entire machine (by using EnvironmentVariableTarget.Machine) seems to require elevated privileges when done from one's application.
you might want to run this
public static void AddToStartup()
{
using (RegistryKey startup = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true))
{
startup.SetValue("Name_of_your_Program", "\"" + Application.ExecutablePath + "\"");
}
}

Webbrowser control ignores FEATURE_BROWSER_EMULATION reg entry

Im developing a custom browser solution with .net's Webbrowser control.
To disable the IE-Compatibilty View, I set the registry entry
Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION:
[Sreenshot regedit] http://zbirk.mirk.at/browserreg.png "Screenshot"
i tried to use the values: dword=8000,dword=8888,dword=9000, but the webbrowser control seems to ignore these reg entries.
Maybe someone had this problems too and may help me.
The WebBrowser control definately DOES respect these keys.
Remember that while taskman may show application.exe in the name column, if you are debugging the exe name is application.vshost.exe
So in my application sI just attempt to create the key every time the app runs. If it fails to create it (because it already exists) then I continue running, if it creates the key then I inform the user that they need to restart the application.
ensure that you are not running within vshost
the app name would be different ie appname.vshost.exe
Thx for your reply, now its working.
Her is my working peace of code:
public void setIEcomp()
{
String appname = Process.GetCurrentProcess().ProcessName+".exe";
RegistryKey RK8 = Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\Internet Explorer\\Main\\FeatureControl\\FEATURE_BROWSER_EMULATION",RegistryKeyPermissionCheck.ReadWriteSubTree);
int value9 = 9999;
int value8 = 8888;
Version ver = webBrowser1.Version;
int value = value9;
try
{
string[] parts = ver.ToString().Split('.');
int vn = 0;
int.TryParse(parts[0], out vn);
if (vn != 0)
{
if (vn == 9)
value = value9;
else
value = value8;
}
}
catch
{
value = value9;
}
//Setting the key in LocalMachine
if (RK8 != null)
{
try
{
RK8.SetValue(appname, value, RegistryValueKind.DWord);
RK8.Close();
}
catch(Exception ex)
{
//MessageBox.Show(ex.Message);
}
}
}
I too could not see that FEATURE_BROWSER_EMULATION made any difference in my application.
I was testing the FEATURE_BROWSER_EMULATION functionality by manually editing the registry with regedit. Nothing I did made any difference. My hosted page was still failing on any new-ish JavaScript and could not load external libraries.
I found my mistake:
I was editing the 64-bit view of the registry with regedit. My app was running as a 32-bit app and looking at the 32-bit view of the registry. That's why my changes to the registry seemed to have no impact on my application. By the way, the WPF project template defaults to "Prefer 32-bit."
Manually editing with regedit within the Wow6432Node key worked:
HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BROWSER_EMULATION
Of course, setting the DWORD value programmatically within your application will also work, since your 32-bit application will edit within the Wow6432Node.
An older post and solution is no longer accurate.
Running procmon and watching for FEATURE_BROWSER_EMULATION shows the following registry variables actually checked. This was for WINWORD.exe but other than that - take your pick...
HKU\S-1-5-21-[my-sid-paws-off]\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION\WINWORD.EXE
HKU\S-1-5-21-[my-sid-paws-off]\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION*
HKLM\SOFTWARE\Microsoft\Office\ClickToRun\REGISTRY\MACHINE\Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION(Default)
HKLM\SOFTWARE\Microsoft\Office\ClickToRun\REGISTRY\MACHINE\Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION\WINWORD.EXE
HKLM\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION\WINWORD.EXE
HKLM\SOFTWARE\Microsoft\Office\ClickToRun\REGISTRY\MACHINE\Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION*
HKLM\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION*

Permission For writing to LOCAL_MACHINE

I made an app that allows windows users to spoof Mac Address .
It works by adding "NetworkAdapter": "00ff00ff00ff" key/value pair to registry of the users selected nic.
The problem is that every time the app tries to make changes to windows registry Windows pop's up a warning dialog, e.g.:
but clicking continue will add the registry values successfully and the app functions normally.
What can i do/or add changes in my code to make the dialog box disappear or can i do it in a better way?
The app requires Admin Privileges
here's the git repo of the app
here's the method:
public void SetMac(string macAddress)
{
const string Name = #"SYSTEM\\CurrentControlSet\\Control\\Class\\{4D36E972-E325-11CE-BFC1-08002bE10318}";
using (RegistryKey key0 = Registry.LocalMachine.OpenSubKey(Name, RegistryKeyPermissionCheck.ReadWriteSubTree, RegistryRights.FullControl))
{
string[] x = key0.GetSubKeyNames();
foreach (string name in x)
{
var var1 = Registry.LocalMachine.OpenSubKey(Name,RegistryKeyPermissionCheck.ReadWriteSubTree,RegistryRights.FullControl);
var v = var1.OpenSubKey(name, RegistryKeyPermissionCheck.ReadWriteSubTree, RegistryRights.FullControl);
var z = v.GetValue("DriverDesc");
if (comboBox1.Text == z.ToString() )
{
v.SetValue("NetworkAddress",comboBox2.Text);
MessageBox.Show(z.ToString());
}
v.Close();
var1.Close();
}
key0.Close();
}
}
You need to run your app under elevated privileges, see Requested registry access is not allowed.
The problem here is that the user does not have permission to open the target key for writing. As abatishchev has already suggested, you need to run the application elevated so that the user actually has Administrators group membership when the code is executed.
The reason that this looks like a CAS permission error is a design flaw in the RegistryKey.OpenSubKey method. It ought to throw an UnauthorizedAccessException when the target key cannot be opened for writing due to inadequate user permissions, but it actually throws a SecurityException instead. The problem ends up appearing to be due to insuffience CAS permissions when it is really the user, not the code, that lacks permissions to edit the key.

Categories

Resources