🙂 İNSANLARIN EN HAYIRLISI INSANLARA FAYDALI OLANDIR 🙂

Ramazan HABER / C# RMOS / tüm formlardaki iconu değiştirme

1-) C# RMOS - tüm formlardaki iconu değiştirme

 

kaynak : https://stackoverflow.com/questions/189031/set-same-icon-for-all-my-forms

 

1. EntryAssemblyInfo.cs

 

using System;

using System.Diagnostics;

using System.Reflection;

using System.Security;

using System.Security.Permissions;

 

namespace WindowsFormsApp4

{

    public static class EntryAssemblyInfo

    {

        private static string _executablePath;

 

        public static string ExecutablePath

        {

            get

            {

                if (_executablePath == null)

                {

                    PermissionSet permissionSets = new PermissionSet(PermissionState.None);

                    permissionSets.AddPermission(new FileIOPermission(PermissionState.Unrestricted));

                    permissionSets.AddPermission(new SecurityPermission(SecurityPermissionFlag.UnmanagedCode));

                    permissionSets.Assert();

 

                    string uriString = null;

                    var entryAssembly = Assembly.GetEntryAssembly();

 

                    if (entryAssembly == null)

                        uriString = Process.GetCurrentProcess().MainModule.FileName;

                    else

                        uriString = entryAssembly.CodeBase;

 

                    PermissionSet.RevertAssert();

 

                    if (string.IsNullOrWhiteSpace(uriString))

                        throw new Exception("Can not Get EntryAssembly or Process MainModule FileName");

                    else

                    {

                        var uri = new Uri(uriString);

                        if (uri.IsFile)

                            _executablePath = string.Concat(uri.LocalPath, Uri.UnescapeDataString(uri.Fragment));

                        else

                            _executablePath = uri.ToString();

                    }

                }

 

                return _executablePath;

            }

        }

    }

}

 

 

2. FormUtils.cs

 

using System.Drawing;

using System.Windows.Forms;

 

namespace WindowsFormsApp4

{

    public static class FormUtils

    {

        public static void SetDefaultIcon()

        {

            var icon = Icon.ExtractAssociatedIcon(EntryAssemblyInfo.ExecutablePath);

            typeof(Form)

                .GetField("defaultIcon", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static)

                .SetValue(null, icon);

        }

    }

}

 

3. Program.cs

 

using System;

using System.Windows.Forms;

 

namespace WindowsFormsApp4

{

    static class Program

    {

        /// <summary>

        /// The main entry point for the application.

        /// </summary>

        [STAThread]

        static void Main()

        {

            Application.EnableVisualStyles();

            Application.SetCompatibleTextRenderingDefault(false);

            FormUtils.SetDefaultIcon();

            Application.Run(new Form1());

        }

    }

}

 

 2021 Ocak 18 Pazartesi
 451