【问题标题】:setup c# windows Application on windows startup在 windows 启动时设置 c# windows 应用程序
【发布时间】:2015-07-19 13:35:19
【问题描述】:

我正在编写一个希望创建不同对话框(是/否类型)等的 Windows 应用程序。但是我想每次 Windows 启动时都运行这个应用程序。但是一旦用户下次登录 Windows 时安装了这个应用程序将在后台进程中自动启动。在 C:\ 目录和应用程序中找到的我的 (ScreenDialog.exe) 需要管理员权限才能使用 c 目录。 但是我使用的代码不起作用。

RegistryKey key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
key.SetValue("ScreenDialog", "C:\\ScreenDialog.exe");

【问题讨论】:

  • 究竟是什么不起作用?没有设置密钥?程序没有启动吗?它会崩溃吗?如果是这样,异常说明了什么?
  • 我认为密钥没有设置好,我尝试通过重新启动 Windows 或关闭来启动,但它不起作用。
  • 你觉得呢?检查注册表并查看...什么版本的 Windows?
  • windows 8.0 64位版本。
  • 我必须在每种类型的窗口上启动它,所以我该怎么办?还是不行

标签: c# winforms c#-4.0 c#-3.0 c#-2.0


【解决方案1】:

我使用自己的类,先检查如下:

  • Windows 版本
  • 以管理员身份运行

根据上述选项你可以在registry*中找到想要的文件夹,如果没有保存在registry中或者发生错误,那么你必须创建一个* *windows 启动文件夹中程序的快捷方式

我的班级代码:

using System;
using System.IO;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Security.Principal;
using Microsoft.Win32;

public static class ComponentController
{
    /// <summary>
    /// Is application running as administrator?
    /// </summary>
    public static Boolean IsAdmin()
    {
        WindowsIdentity identity = WindowsIdentity.GetCurrent();

        if (identity != null)
            return (new WindowsPrincipal(identity)).IsInRole(WindowsBuiltInRole.Administrator);

        return false;
    }

    public static Boolean IsWindowsVistaOrHigher()
    {
        OperatingSystem os = Environment.OSVersion;
        return ((os.Platform == PlatformID.Win32NT) && (os.Version.Major >= 6));
    }

    /// <summary>
    /// Add executable file of this app to registry startup path:
    /// 'LocalMachine\SOFTWARE\Microsoft\Windows\CurrentVersion\Run'
    /// </summary>
    /// <param name="targetEveryone">Run as administrator</param>
    public static void AddToStartup(Boolean targetEveryone)
    {
        try
        {
            using (RegistryKey main = (targetEveryone & IsAdmin() ? Registry.LocalMachine : Registry.CurrentUser))
            {
                using (RegistryKey key = main.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true))
                {
                    String fileName = Path.GetFileNameWithoutExtension(Application.ExecutablePath);

                    if (key.GetValue(fileName) == null)
                        key.SetValue(fileName, Application.ExecutablePath);
                }
            }
        }
        catch (Exception ex)
        {
            // report exception ...

            //
            // Copy Shortcut To CommonStartUp or StartUp
            //
            try
            {
                Environment.SpecialFolder folder = ((targetEveryone && IsWindowsVistaOrHigher()) ? Environment.SpecialFolder.CommonStartup : Environment.SpecialFolder.Startup);
                String fileDestination = Path.Combine(Environment.GetFolderPath(folder), Path.GetFileNameWithoutExtension(Application.ExecutablePath)) + ".lnk";

                if (!File.Exists(fileDestination))
                    Shortcut.Create(fileDestination, Application.ExecutablePath, null, null, "description...", null, null);
            }
            catch (Exception exp)
            {
                // report exception ...
            }
        }
    }


    public class Shortcut
    {
        private static Type m_type = Type.GetTypeFromProgID("WScript.Shell");
        private static object m_shell = Activator.CreateInstance(m_type);

        [ComImport, TypeLibType((short)0x1040), Guid("F935DC23-1CF0-11D0-ADB9-00C04FD58A0B")]
        private interface IWshShortcut
        {
            [DispId(0)]
            string FullName { [return: MarshalAs(UnmanagedType.BStr)] [DispId(0)] get; }

            [DispId(0x3e8)]
            string Arguments { [return: MarshalAs(UnmanagedType.BStr)] [DispId(0x3e8)] get; [param: In, MarshalAs(UnmanagedType.BStr)] [DispId(0x3e8)] set; }

            [DispId(0x3e9)]
            string Description { [return: MarshalAs(UnmanagedType.BStr)] [DispId(0x3e9)] get; [param: In, MarshalAs(UnmanagedType.BStr)] [DispId(0x3e9)] set; }

            [DispId(0x3ea)]
            string Hotkey { [return: MarshalAs(UnmanagedType.BStr)] [DispId(0x3ea)] get; [param: In, MarshalAs(UnmanagedType.BStr)] [DispId(0x3ea)] set; }

            [DispId(0x3eb)]
            string IconLocation { [return: MarshalAs(UnmanagedType.BStr)] [DispId(0x3eb)] get; [param: In, MarshalAs(UnmanagedType.BStr)] [DispId(0x3eb)] set; }

            [DispId(0x3ec)]
            string RelativePath { [param: In, MarshalAs(UnmanagedType.BStr)] [DispId(0x3ec)] set; }

            [DispId(0x3ed)]
            string TargetPath { [return: MarshalAs(UnmanagedType.BStr)] [DispId(0x3ed)] get; [param: In, MarshalAs(UnmanagedType.BStr)] [DispId(0x3ed)] set; }

            [DispId(0x3ee)]
            int WindowStyle { [DispId(0x3ee)] get; [param: In] [DispId(0x3ee)] set; }

            [DispId(0x3ef)]
            string WorkingDirectory { [return: MarshalAs(UnmanagedType.BStr)] [DispId(0x3ef)] get; [param: In, MarshalAs(UnmanagedType.BStr)] [DispId(0x3ef)] set; }

            [TypeLibFunc((short)0x40), DispId(0x7d0)]
            void Load([In, MarshalAs(UnmanagedType.BStr)] string PathLink);

            [DispId(0x7d1)]
            void Save();
        }

        public static void Create(string fileName, string targetPath, string arguments, string workingDirectory, string description, string hotkey, string iconPath)
        {
            IWshShortcut shortcut = (IWshShortcut)m_type.InvokeMember("CreateShortcut", System.Reflection.BindingFlags.InvokeMethod, null, m_shell, new object[] { fileName });

            shortcut.Description = description;
            shortcut.TargetPath = targetPath;
            shortcut.WorkingDirectory = string.IsNullOrEmpty(workingDirectory) ? targetPath : workingDirectory;
            shortcut.Arguments = arguments;

            if (!string.IsNullOrEmpty(hotkey)) shortcut.Hotkey = hotkey;

            if (!string.IsNullOrEmpty(iconPath)) shortcut.IconLocation = iconPath;
            else shortcut.IconLocation = System.Reflection.Assembly.LoadFile(targetPath).Location.Replace('\\', '/');

            shortcut.Save();
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多