When using "Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(GuidAttribute), false).GetValue(0)).Value" in C#, an "Index was outside the bounds of the array" error occurs

Reza Jaferi 331 Reputation points
2022-08-30T21:24:11.033+00:00

To run a single instance of a program, I use the following approach, which is recommended in this link rather than the approach presented here.

C#:

using System;  
using System.Reflection;  
using System.Runtime.InteropServices;  
using System.Security.AccessControl;  
using System.Security.Principal;  
using System.Threading;  
using System.Windows;  
  
namespace My_App  
{  
    /// <summary>  
    /// Interaction logic for App.xaml  
    /// </summary>  
    static class GlobalMutex  
    {  
        public static void SingleGlobalInstance()  
        {  
            string AppGuid = ((GuidAttribute)Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(GuidAttribute), false).GetValue(0)).Value.ToString();  
            string MutexId = string.Format("Global\\{{{0}}}", AppGuid);  
            bool CreatedNew;  
            var AllowEveryoneRule = new MutexAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null), MutexRights.FullControl, AccessControlType.Allow);  
            var SecuritySettings = new MutexSecurity();  
            SecuritySettings.AddAccessRule(AllowEveryoneRule);  
            using (var mutex = new Mutex(false, MutexId, out CreatedNew, SecuritySettings))  
            {  
                var HasHandle = false;  
                try  
                {  
                    try  
                    {  
                        HasHandle = mutex.WaitOne(5000, false);  
                        switch (HasHandle)  
                        {  
                            case false:  
                                throw new TimeoutException("Timeout waiting for exclusive access");  
                        }  
  
                    }  
                    catch (AbandonedMutexException)  
                    {  
                        HasHandle = true;  
                    }  
                }  
                finally  
                {  
                    switch (HasHandle)  
                    {  
                        case true:  
                            mutex.ReleaseMutex();  
                            break;  
                    }  
                }  
            }  
        }  
    }  
  
    public partial class App : Application  
    {  
        protected override void OnStartup(StartupEventArgs e)  
        {  
            GlobalMutex.SingleGlobalInstance();  
            base.OnStartup(e);  
        }  
    }  
}  

But I get the following error:

Index was outside the bounds of the array

at line 5.

What is the cause of this error and how can it be resolved?
I use the following tools:

  • NET Framework 4.5
  • WPF

Thank you for your time.
Best regards

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,667 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,204 questions
XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
762 questions
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 111.8K Reputation points
    2022-08-31T07:38:02.88+00:00

    Try adding this line:

    [assembly: Guid( "F6341D4D-5A4D-429F-B587-28CD958B27F2" )]

    to any source file, outside of classes and namespaces (or to AssemblyInfo.cs file if your project contains it).

    2 people found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Jiale Xue - MSFT 30,811 Reputation points Microsoft Vendor
    2022-08-31T09:37:08.07+00:00

    Hi @Reza Jaferi ,

    string AppGuid = ((GuidAttribute)Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(GuidAttribute), false).GetValue(0)).Value.ToString();

    This code is for get application GUID as defined in AssemblyInfo.cs.

    It could not be obtained in debug mode.

    Please try to debug in release mode.

    ![![236488-image.png

    Best Regards,
    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.