在Visual Studio Designer中找不到自定义字体的C#文件 [英] C# File Not Found For Custom Font in Visual Studio Designer

查看:136
本文介绍了在Visual Studio Designer中找不到自定义字体的C#文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C#的新手,所以我的WinForms项目中几乎包含一个包含自定义字体的文件夹.字体在启动应用程序时绝对可以正常工作,但是在Visual Studio本身的实际设计器中,无法加载自定义控件(因为在设计器中,我使用的路径是不同的).这是我用来从资源文件夹中找到字体的代码:

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 application, but in the actual designer on Visual Studio itself, the custom control can't be loaded (as the path I'm using is different when in the designer). Here is the code I use to locate the font from the resources folder:

string leadDirectory = System.IO.Path.Combine(System.Windows.Forms.Application.StartupPath, "Resources" + Path.DirectorySeparatorChar + "Roboto-Regular.ttf");

设计器中使用的目录是"C:\ Program Files(x86)\ Microsoft Visual Studio \ 2017 \ Community \ Common7 \ IDE \ Resources \ Roboto-Regular.ttf",并且显然它抛出的文件不是在方法中发现异常.

The directory which is used in the designer is "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\Resources\Roboto-Regular.ttf", and obviously it throws a file not found exception in the method.

任何帮助将不胜感激!

推荐答案

您没有打开您认为要打开的目录.在调试模式下检查什么System.Windows.Forms.Application.StartupPath实际上 .

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

要完全了解这里出了什么问题,您应该了解命令提示符下正在运行的程序的工作方式.您具有命令提示符,并且在该提示符下,您看到当前路径"设置为某个目录.通常,这表明您想在该特定目录中执行操作,但是您可以通过提供完整的启动路径来启动系统上任何位置的程序.但是,这将不会使您的命令提示符切换到该启动的应用程序的路径.即使运行位于其他位置的程序,您仍将保留在同一文件夹中.该文件夹是您正在使用的启动路径".可以想象,它与您要查找该Resources文件夹的位置完全没有关系.

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.

尽管已经发展为图形用户界面,但是程序启动的方式仍然与DOS中的方式相同,因此这种区别仍然存在.

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.

在Windows窗体中,可以使用Application.ExecutablePath来获取exe文件的完整路径和文件名,因此,如果在此文件上使用Path.GetDirectoryName(),则具有所需的基本路径.如果您的程序不是不是WinForms应用程序,则可以改用System.Reflection名称空间中的Assembly.GetExecutingAssembly().Location.

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.

这篇关于在Visual Studio Designer中找不到自定义字体的C#文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆