C# File Not Found For Custom Font in Visual Studio Designer

前端 未结 1 1984
[愿得一人]
[愿得一人] 2021-01-24 09:31

I\'m relatively new to C#, so pretty much I have a folder included in my WinForms project that contains a custom font. The font works absolutely fine upon launch of the applicat

1条回答
  •  说谎
    说谎 (楼主)
    2021-01-24 10:10

    You are not opening the directory you think you are opening. Check in debug mode what System.Windows.Forms.Application.StartupPath actually is.

    To fully understand what goes wrong here, you should understand how running programs from the command prompt work. You have the command prompt, and on that prompt, you see your "current path" is set to a certain directory. Normally, this indicates you want to do stuff in that particular directory, but you can launch a program that is anywhere on your system by giving the full path to launch. However, this will not make your command prompt switch to the path of that launched application. You will still remain in that same folder, despite running a program that is located somewhere else. This folder is the "startup path" that you are using. As you can imagine, it has no relation at all to where you are trying to look for that Resources folder.

    Despite having evolved to a graphical user interface, the way programs launch still works the same way as it did in DOS, so this distinction remains.

    In Windows Forms, you can use Application.ExecutablePath to get the full path and filename to your exe file, so if you use Path.GetDirectoryName() on that, you have the base path you want. In case your program would not be a WinForms application, you can instead use Assembly.GetExecutingAssembly().Location, from the System.Reflection namespace.

    0 讨论(0)
提交回复
热议问题