Question

I'm writing a small TaskExplorer which saves all running processes to an .ini file. It creates an folder called "Überwachung" in the current user's desktop and saves the files with date as name.

I want this program to startup after a user logs in, but I keep getting a system.unauthorizedaccessexception while copying the current running assembly to the windows startup.

Here is my copy code:

if (!File.Exists(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Startup), "ProzessManager")))
            File.Copy(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Startup), "ProzessManager"),false);
        File.SetAttributes(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), FileAttributes.Normal);
Was it helpful?

Solution

You need the folder StartUp for All Users

using System.IO;
using System.Reflection;

private string pathStartUp = Environment.GetFolderPath(Environment.SpecialFolder.CommonStartup);

var exe = Assembly.GetExecutingAssembly().Location;
var destiny = Path.Combine(pathStartUp, Path.GetFileName(exe));
var data = File.ReadAllBytes(exe);
File.WriteAllBytes(destiny, data);

It's simpler to just read and write all the bytes of the main app.

EDIT: Changed the variable pathStartUp to use GetFolderPath, so it works even on Windows XP, which has a diferente startup path than Windows Vista / 7 / 8.

OTHER TIPS

You need to authorize you application with an administrator user. First add a new Application Manifest File to your project, then change the following line

<requestedExecutionLevel level="asInvoker" uiAccess="false" />

to

<requestedExecutionLevel  level="requireAdministrator" uiAccess="false" />
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top