0

I have used every suggested methods from this following topic but no luck.

Test if a Font is installed

When I debug, it always skip after the "If" condition line without checking. I don't know why.

For example:

string fontName = "Consolas";
float fontSize = 12;

using (Font fontTester = new Font( 
       fontName, 
       fontSize, 
       FontStyle.Regular, 
       GraphicsUnit.Pixel)) 
{
    if (fontTester.Name == fontName) <<<<< [1] 
    {
        // Font exists
    }
    else
    {
        // Font doesn't exist
    }
} <<<<< [2]

After I marked and pressed F10/F11 on [1] line, it always go to [2] without checking.

This is my actual code:

private static void Main(string[] args)
{
    if (args.Length == 0)
    {
        args = new string[] { AppDomain.CurrentDomain.BaseDirectory + System.Reflection.Assembly.GetExecutingAssembly().GetName().Name + ".txt" };
    }
    if (args.Length > 0)
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);

        string fontName = "Free 3 of 9 Extended";
        float fontSize = 12;

        using (Font fontTester = new Font( 
               fontName, 
               fontSize, 
               FontStyle.Regular, 
               GraphicsUnit.Pixel)) 
        {
             if (fontTester.Name == fontName)
             {
                 // Font exists
             }
             else
             {
                 // Font doesn't exist
             }
        }

        Application.Run(new MenuForm());
    }
}

UPDATED:

I checked my project properties again and unchecked "Optimize Code" in "Build" tab then it's worked.

Mike 'Pomax' Kamermans
  • 49,297
  • 16
  • 112
  • 153
tumsd923
  • 28
  • 6
  • So... what is the value of `fontTester.Name`? – Alexei Levenkov Feb 28 '20 at 01:12
  • 1
    AFAIK, there's no way that it would jump from `[1]` to `[2]` unless an exception was thrown. Are you sure the code you provided is **the exact same code** that you used for testing? Are you using any `try..catch` statements? Did you [make sure that no exceptions were thrown](https://stackoverflow.com/a/1698165/8967612)? – 41686d6564 stands w. Palestine Feb 28 '20 at 01:21
  • @AlexeiLevenkov If "Consolas" in not installed, "fontTester.Name" will be "Microsoft Sans Serif" by default. – tumsd923 Feb 28 '20 at 01:28
  • @AhmedAbdelhameed I have tried with "try-catch" but nothing happened also. Is it the problem with my IDE? – tumsd923 Feb 28 '20 at 01:29
  • 1
    @tumsd923 I'm saying you should actually remove any try-catch statements and make sure that no exceptions are being thrown. If the code you showed here isn't the same you used for testing, please [edit] the question and include the same code and make sure it's a [repro]. – 41686d6564 stands w. Palestine Feb 28 '20 at 01:38
  • 1
    As @AhmedAbdelhameed said there is no way one of the branches of `if` would be skipped... Note that if you are debugging optimized/Release build and there is no actual code in the branches of if (like shown in the post) debugger will "skip" lines that has no code generated for them leading to behavior you claim. Since general expectation on SO is that the author of the question misleads about what actual code they are debugging vs. code posted in the question it is very hard to guess what is happening for sure. – Alexei Levenkov Feb 28 '20 at 01:44
  • @tumsd923 Please start a new empty project and paste that code into it and let us know if it reproduces the behavior you described (it doesn't for me). If you can't get the same behavior in the new/empty project, it's not a [repro]. – 41686d6564 stands w. Palestine Feb 28 '20 at 02:15
  • @AhmedAbdelhameed I did check my project properties again and I unchecked "Optimize Code" in "Build" tab then it's just worked. Sorry for this silly mistake but what does this "Optimize Code" mean ? – tumsd923 Feb 28 '20 at 02:21
  • @tumsd923 See [this](https://stackoverflow.com/q/2444467/8967612) and [this](https://stackoverflow.com/q/113866/8967612). – 41686d6564 stands w. Palestine Feb 28 '20 at 02:23
  • Based on your last edit, it sounds like you should probably delete this question, if all you forgot to do was make sure your code ran "as is". – Mike 'Pomax' Kamermans Feb 28 '20 at 20:15

0 Answers0