I can see the app in registers but it wont startup.
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("WindowsSelfRepairTool", System.Reflection.Assembly.GetEntryAssembly().Location);
This is the code I copied from internet, it gets my application to registers because I can see it "On startup" in task manager as well as in the registry editor. But for some reason, when I restart the computer the app wont start.
Also I tried this code in Console App and added code Process.Launch("..") to launch the WPF one. Console starts successfuly but it doesnt run the WPF.. Any help?
Related
I am trying to create environment variables in Windows through a UWP application using the SetEnvironmentVariable method, but it seems that UWP as it runs in a sandbox prevents the application from being able to create environment variables.
So I have created a console app to call it from UWP:
var value = Environment.GetEnvironmentVariable("Test1", EnvironmentVariableTarget.User);
if (value == null)
{
var path = "pathTest";
Environment.SetEnvironmentVariable("Test1", path, EnvironmentVariableTarget.User);
Console.WriteLine("The environment variable has been created successfully");
}
else
{
Console.WriteLine("Environment variable already exists");
}
UWP code to run the console application that I have added in the application package:
public class TestService : ITestService
{
public async Task ExecuteSampleApp()
{
await FullTrustProcessLauncher.LaunchFullTrustProcessForCurrentAppAsync();
}
}
and I have added the .exe in the same UWP application package, and I have added the necessary permissions (runFullTrust) in the Package.appxmanifest to be able to run external applications (that are in the application package), up to this point everything is fine , I can run the console app from my UWP app but it seems that the console app when run from the UWP app can't create the environment variable it's like access is taken away (I think somehow the app sandbox UWP adds the same security restrictions even to the .exe added in the application package), however if I run the same console application in the bin folder that is created when compiling the UWP app it works, the problem occurs only when the UWP app calls the .exe. Anyone have any other ideas on how I could create environment variables in UWP either with this approach I was following or with another?
And with this console app, would it be possible to run cmd commands bypassing the UWP sandbox security restrictions?
Answer to #RoyLi-MSFT:
No, there are no exceptions, the UWP application executes the console application and it shows the message "Environment variable already exists" detecting as if it already exists, however it has not created it at any time, it seems that the console application when it is executed by the UWP app it is not able to really obtain if the environment variable exists or not in the operating system and it returns an incorrect state (indicating that it exists when it does not). However, if the console application is run regardless, it does work, which suggests that it is a UWP problem that somehow isolates the console application to the same sandbox as the UWP app when it is executed by the UWP app. Regarding your other question with placing the exe file in the application package, I mean that I add in the Assets folder the console application in charge of registering the environment variables to be able to execute it with FullTrustProcessLauncher through UWP since it is the only way of running a win32 app from a sandbox environment.
2ยบ Answer to #RoyLi-MSFT:
I use EnvironmentVariableTarget.User in my code, the code I had put here was an example (I have updated the example to clarify it), but I tried to use the 3 available enumerations: Process, User and Machine, the User type is the one that works correctly and is the one that I use in my console application, but as I mentioned before, when the UWP application is the one running the console application it cannot create the environment variable and the console shows: "The environment variable already exists" despite using EnvironmentVariableTarget.User.
I clarify this point: when the console application is run manually if it works, it only shows the message "The environment variable already exists" when the UWP application is in charge of executing it with FullTrustProcessLauncher.
Based on the document-SetEnvironmentVariable(String, String), the Environment Variable you created is stored in the current process, which means the process environment block. I tested your code inside of a console app. Every time when I launched the console app, it shows The environment variable has been created successfully. If I tried to read the value after I write it, I could get the correct value. This behavior matches the document.
So the behavior you mentioned should be expected. You could try directly read the Environment Variable when you haven't closed the console app and you should be able to get the value you set. And #jerry's comment should make sense, you might need to try SetEnvironmentVariable(String, String, EnvironmentVariableTarget) with other options like EnvironmentVariableTarget.User.
Update:
Based on your previous comment, you mentioned that you put the .exe file in the Assets folder of your UWP app. I would suggest you using the Windows Application Package Project to package the console app together with the UWP app. In that way under my test, the console app will correctly add the Environment Variable to the current user block and you could get it as you want.
Here are the steps:
Create a new console app under the solution of your UWP app. Add the code you need.
Create a Windows Application Package Project, and add the console app and the UWP as application reference to the Package Project.
Declare the extension in the package manifest of the Package Project
launch it from the UWP app.
You could refer Stafen's blog for more details.
And the result:
In the end I managed to create the environment variables. Through the UWP application we have to execute the console application and through the console application execute the CMD setx command through the Process class, this is the code:
Process cmd = new Process();
cmd.StartInfo.FileName = "cmd.exe";
cmd.StartInfo.RedirectStandardInput = true;
cmd.StartInfo.RedirectStandardOutput = true;
cmd.StartInfo.CreateNoWindow = true;
cmd.StartInfo.UseShellExecute = false;
cmd.Start();
var testPath = "c:\...";
cmd.StandardInput.WriteLine(#"setx testName + " \"" + testPath + "\"");
And with this we manage to bypass the restrictions that UWP has in the sandbox in which the applications are executed:
I am trying to add my application to StartUp, when I use my code to add an application to startup registry, it gets added but when I check in msconfig or taskmgr its disabled by default. Any idea how to fix this? Tested on Windows 8 and 8.1
RegistryKey rk = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
rk.SetValue("TestApp", Application.ExecutablePath);
rk.Close();
Should I try to create a shortcut in shell:startup instead?
Thank you for help.
I know how to tell the registy to run my app on startup.
There are a lot of threads about it.
I used this one:
How do I set a program to launch at startup.
The problem I face is:
I give the user the option to disable/enable this option
when the user uninstall,
there is a key left in the registry that tells the app to run (although the app was already uninstalled)
I know I can handle it by a shortcut in the startup folder, but I tried it already and I have problem with it too.
Thanks
Eli
To remove the entry in the registry you should instruct your installation program to remove the registry value.
If you are using the build in installation project in VS you could do that by implementing the Uninstall event.
If it is a click once installation you just have to ignore it.
Simple solution without using Custom Actions
I just added a registry Key by the Registry editor until I got to the Run folder and added to it the value "name of my app" (on the right panel) and I didn't wrote a path just left it ""
I played with the checkbox (add key, delete key)
and even though I left it with the key. when I uninstalled it was gone.
I'm running Win7 Pro x64.
I made a small console application, which gets the parameters from the command line and makes some actions based on those parameters and the config from registry.
The main goal is it should be portable, I mean one EXE file.
I wanted to make something like "setup" - on the user's demand, the app should fill the registry with default config values, which can be changed later. So I decided that my app should run "setup" when user starts it with a parameter. Let's say it should be
myapp.exe --setup
What I need to do is (in a short way):
RegistryKey k = Registry.CurrentUser.OpenSubKey("SOFTWARE\\something\\else");
// the key exists, it's checked elsewhere
k.SetValue(optionName, optionValue);
When I did this, it threw an exception System.UnauthorizedAccessException. OK, I caught it so I could inform user with MessageBox that he should run it as an Administrator.
PROBLEM 1: How to easily run the app with commandline argument as an Administrator?
I mean, will every user need to run it from RUNAS /user:userName "myapp.exe --setup" ? Is there simplier method to do it (simple like RMB->Run as Administrator but with parameter)?
I tried making .bat files with myapp.exe --setup and run them as Administrator, but it needed to have the whole path to the EXE, which is totally not portable.
OK, there's another one thing. I finally ran it with RUNAS and there was exception thrown (catched). Why? I made a simple test and turned off the "--setup" switch - so the app should run "setup" without any commandline parameters. I clicked it with RMB and choose "Run as Administrator" - the same! So:
PROBLEM 2: Why running as Administrator doesn't give me the permissions to create/edit keys in registry?
My domain account is in "Administrators" group and I've never had any problems with that.
i have replaced windows shell with my application it worked perfectly, after closing my application i have to launch windows explorer with the following piece of code
Code to start explorer
Process.Start(#"c:\windows\explorer.exe");
Registry key i have used to replace shell
HKEY_Local_Machine\Software\Microsoft\WindowsNT\CurrentVersion\WinLogon\Shell
it doesnt show taskbar and start menu, it just shows mydocuments folder. I need start menu and taskbar while after explorer started
My guess is that since explorer isn't defined as the shell, it won't run as the shell. I think that you'd have to change the registry settings to make explorer the shell again before you run it.
So, as you wrote you replaced the shell in the registry with your own version. So it's up to you to show a start menu, etc. If you like to start a explorer and let it act as a shell, go on and replace the entry in the registry with the old one.
Due to the fact, that you like to be the shell again, next time windows starts, maybe the following trick will do it:
Prerequisities:
Place your program in registry as shell and start windows
Your program runs and wants to start explorer as shell
Action to to:
Replace entry in registry against entry containing explorer as shell
Start the explorer
Replace entry in registry back to your app as shell programm
Wait till next boot...
You will probably have to kill the existing shell process (i.e. your app) before starting explorer again.