i have following small line of code

RegistryKey rkApp = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run",true);


                rkApp.SetValue("MyApp1", "C:\\WINDOWS\\myexe");

now when i run code in c# (desktop application ) it make changes in registeries and .exe run when system start ..but the problem is whne i make setup and deploymemt of same code and deploy on server ..and install desktop application on server ..it does not make any entries .. any help

do let me know when further EXPLANATION is required ...

i have following small line of code

Perhaps the user on the server does not have priviliges to modify the registry.

let me share one more thing the same issue ocurring via setup/deployment on my machine ..I mean via an exe it creating problem on my machine too ..but whne i make entries via code its work FINE ..

Perhaps the user on the server does not have priviliges to modify the registry.

An exception should be thrown in that case, but you can verify permissions without much difficulty using the RegistryPermission class. Here's a helper class that does the grunt work (for cleanliness):

using System.Security;
using System.Security.Permissions;

public static class RegistryInfo
{
    public static bool HasAccess(RegistryPermissionAccess accessLevel, string key)
    {
        try
        {
            new RegistryPermission(accessLevel, key).Demand();
            return true;
        }
        catch (SecurityException)
        {
            return false;
        }
    }

    public static bool CanWrite(string key)
    {
        return HasAccess(RegistryPermissionAccess.Write, key);
    }

    public static bool CanRead(string key)
    {
        return HasAccess(RegistryPermissionAccess.Read, key);
    }
}

And from there just call either CanRead() or CanWrite() to check for access on a key:

using System;
using Microsoft.Win32;

class Program
{
    static void Main(string[] args)
    {
        string key = @"HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Run";
        string value = "MyApp1";

        if (RegistryInfo.CanRead(key))
        {
            Console.WriteLine("Previous value '{0}': '{1}'",
                value,
                Registry.GetValue(key, value, "(null)"));
        }
        else
        {
            Console.WriteLine("No permission to read key: '{0}'", key);
        }

        if (RegistryInfo.CanWrite(key))
        {
            Registry.SetValue(key, "MyApp1", "C:\\WINDOWS\\myexe");
        }
        else
        {
            Console.WriteLine("No permission to write key: '{0}'", key);
        }

        if (RegistryInfo.CanRead(key))
        {
            Console.WriteLine("Updated value '{0}': '{1}'",
                value,
                Registry.GetValue(key, value, "(null)"));
        }
        else
        {
            Console.WriteLine("No permission to read key: '{0}'", key);
        }
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.