Enable startup application programmatically - c#

I 've an aplication which puts another application in startup.
I've put the latter exe in startup using the following
code:
string strExeFilePath = System.Reflection.Assembly.GetExecutingAssembly().Location;
string strWorkPath = System.IO.Path.GetDirectoryName(strExeFilePath);
using (RegistryKey key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true))
{
key.SetValue("my_service_name", strWorkPath + "\\MyProgram.exe");
}
It successfully put the entry in regedit. 'my_service_name' is listed in the 'startup' tab of the Task Manager also, But its in 'Disabled' state.
How can I enable it programmatically while adding to regedit? Why is it not 'Enabled' by default?.

Related

C# Folder browse Dialog not showing Network shared folders win10

I have created an application(windows) compiled with .NET 4.6.1 and used the FolderBrowserDialog object. When a button is pressed I execute this code:
FolderBrowserDialog folderbrowserdialog = new FolderBrowserDialog();
folderbrowserdialog.Description = "Custom Description";
if (folderbrowserdialog.ShowDialog() == DialogResult.OK)
{
filePath = folderbrowserdialog.SelectedPath ;
}
what i get from the folderbrowserdialog(like foto)
however ,the folder browserdialog is not showing the networks shared folder(that the purpose of my app) otherewise just the pc folders.
but what i want to get it is the network shared folders which could i also access from windows 10 like foto here:
notes to be marked:
i could not use the open file dialog cause i need the folder location.
i desgined the Appto be opened just like admin by adding manisfest so the app is always starting like admin.
the app should be comptiable with windows 10,7
note i know that i could try setting this registry option (could be broken in Win10):
HKEY_LOCAL_MACHINE/SOFTWARE/Microsoft/Windows/CurrentVersion/Policies/System
EnableLinkedConnections=1
but it does not make a sense to add this registry by every customer PC
so is there any tipps to show the network shared folders in FolderBrowserDialog ?
Finally after reading many topics i found that the only solution is to add a Registry key programmatically so here how to add specfic C# Registry Subkey with dword value:
i wrote a method wich could all use it
just to let you know after using it you have to restart the device after it ,it will work ;)
public void ConfigureWindowsRegistry()
{
RegistryKey localMachine = RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.LocalMachine, RegistryView.Registry64); //here you specify where exactly you want your entry
var reg = localMachine.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System", true);
if (reg == null)
{
reg = localMachine.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System", true);
}
if (reg.GetValue("EnableLinkedConnections") == null)
{
reg.SetValue("EnableLinkedConnections", "1", RegistryValueKind.DWord);
MessageBox.Show(
"Your configuration is now created,you have to restart your device to let app work perfektly");
}
}
I had the same issue. The reason of the problem: I was using as an Administrator. The mapped drives are related to the user, so I tried to use as an normal user and I could see the mapped drives.

How to pass path as parameter to a custom made context menu option?

I have created a custom context menu option. This option would remove the file from where it is called. The custom context menu was successfully added and also it calls my remove application. But the problem is i dont know how to pass the path to remove.exe of the file from where it is called.
Here is my custom context menu code
RegistryKey _key = Registry.ClassesRoot.OpenSubKey("*\\Shell", true);
RegistryKey newkey = _key.CreateSubKey("Remove");
newkey.SetValue("AppliesTo", "under:T:");
RegistryKey subNewkey = newkey.CreateSubKey("Command");
subNewkey.SetValue("", "C:\\remove.exe");
subNewkey.Close();
newkey.Close();
_key.Close();
Change this line:
subNewkey.SetValue("", "C:\\remove.exe");
to this:
subNewkey.SetValue("", "C:\\remove.exe %1");
The OS sends the filename of the clicked on file as %1.

StreamWriter Access Denied on Windows Startup [duplicate]

I am using registry key to set my application to load on Windows Startup(after a user login).
My Code:
RegistryKey RegKey = Registry.LocalMachine;
RegKey = RegKey.OpenSubKey(#"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);
RegKey.SetValue("AppName", "\"" + #"C:\Users\Name\Desktop" + "\"");
RegKey.Close();
So with this code, my application load at startup, however the working directory is
C:\Windows\System32
Does anyone know why ?
This does not work for me because that program needs couple of files within the same directory as that one to operate. If the program loaded on my chosen directory("C:\Users\Name\Desktop") then the problem would not exist.
Anyone has any suggestion for this ?
Directory.SetCurrentDirectory() can be used to set your working directory when the app starts. EXE path can be retrieved using Application.ExecutablePath.
Put them together:
var fi = new FileInfo(Application.ExecutablePath);
Directory.SetCurrentDirectory(fi.DirectoryName);
I've figured out a cheap trick on how to accomplish this.
When your application starts up, Read the registry again to get your application's start-up path(the one you intended).
For example: Appl1 has a startup path of "C:\Users\Name\Desktop\App1.exe".
Once you read the registry for that path, set that as current directory.
Something like this:
RegistryKey RegKey = Registry.LocalMachine;
RegKey = RegKey.OpenSubKey(#"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", false);
string Path = RegKey.GetValue("App1.exe").ToString();
Path = Path.Replace(#"\App1.exe"", ""); // Now it's a valid directory.
Directory.SetCurrentDirectory(Path);
This worked for me but if anyone has a better method, I would love to hear them.

Starting application on start-up, using the wrong path to load

I am using registry key to set my application to load on Windows Startup(after a user login).
My Code:
RegistryKey RegKey = Registry.LocalMachine;
RegKey = RegKey.OpenSubKey(#"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);
RegKey.SetValue("AppName", "\"" + #"C:\Users\Name\Desktop" + "\"");
RegKey.Close();
So with this code, my application load at startup, however the working directory is
C:\Windows\System32
Does anyone know why ?
This does not work for me because that program needs couple of files within the same directory as that one to operate. If the program loaded on my chosen directory("C:\Users\Name\Desktop") then the problem would not exist.
Anyone has any suggestion for this ?
Directory.SetCurrentDirectory() can be used to set your working directory when the app starts. EXE path can be retrieved using Application.ExecutablePath.
Put them together:
var fi = new FileInfo(Application.ExecutablePath);
Directory.SetCurrentDirectory(fi.DirectoryName);
I've figured out a cheap trick on how to accomplish this.
When your application starts up, Read the registry again to get your application's start-up path(the one you intended).
For example: Appl1 has a startup path of "C:\Users\Name\Desktop\App1.exe".
Once you read the registry for that path, set that as current directory.
Something like this:
RegistryKey RegKey = Registry.LocalMachine;
RegKey = RegKey.OpenSubKey(#"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", false);
string Path = RegKey.GetValue("App1.exe").ToString();
Path = Path.Replace(#"\App1.exe"", ""); // Now it's a valid directory.
Directory.SetCurrentDirectory(Path);
This worked for me but if anyone has a better method, I would love to hear them.

Insert a Context menu Item to Explorer and pass the Full File Name when Context Menu item is Clicked

I want to add a context menu item to the windows explorer context menu for all "jpg" files .The name of the context menu item will be 'process JPEG' when the user clicks this,a main executable will be invoked.The problem is i have already created the main c# executable and its not run only via context menu it can be run independently.I want to pass the file name or file-names which the user select and clicks the context menu command and to the main executable,so that the main executable will be able to get the files using some method and process those.I have done the following to integrate the context menu-Please help me out
public static void Register(
string fileType, string shellKeyName,
string menuText, string menuCommand)
{
Debug.Assert(!string.IsNullOrEmpty(fileType) &&
!string.IsNullOrEmpty(shellKeyName) &&
!string.IsNullOrEmpty(menuText) &&
!string.IsNullOrEmpty(menuCommand));
// create full path to registry location
string regPath = string.Format(#"{0}\shell\{1}", fileType, shellKeyName);
// add context menu to the registry
using (RegistryKey key = Registry.ClassesRoot.CreateSubKey(regPath))
{
key.SetValue(null, menuText);
}
// add command that is invoked to the registry
using (RegistryKey key = Registry.ClassesRoot.CreateSubKey(
string.Format(#"{0}\command", regPath)))
{
key.SetValue(null, menuCommand);
}
}
To register a verb for a file type on older versions of Windows you would:
Get the ProgID. In your case, read the default value under HKCR\.jpg (This normally gives you something like jpgfile)
Write your verb command under HKCR\%ProgId%\shell\%YourVerb%\command
Starting with XP you can register supplemental verbs under SystemFileAssociations without having control of the ProgID:
Write your verb command under HKCR\SystemFileAssociations\.jpg\shell\%YourVerb%\command
The verb command is usually "c:\full\path\to\yourapp.exe" "%1" and %1 is replaced with the filename by windows. Your application has to parse the commandline.
To properly support opening several files at the same time it might be a good idea to use DDE or IDropTarget (Or IExecuteCommand on Win7+)

Categories