且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在Visual Studio Designer中找不到自定义字体的C#文件

更新时间:2022-12-11 14:45:58

您没有打开您认为要打开的目录.在调试模式下检查什么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.