简体   繁体   English

使用目标框架3.5从控制台应用程序访问Fonts文件夹

[英]Accessing Fonts folder from console application with target framework 3.5

I need to get the path of a specific font in my c:/windows/ folder The below code works perfectly when the target framework is 4.0 But my application can target only 3.5 and i need to use this in a console application c# 我需要在我的c:/ windows /文件夹中获取特定字体的路径当目标框架为4.0时,以下代码可以完美运行,但我的应用程序只能以3.5为目标,因此我需要在控制台应用程序c#中使用它

How can i achieve this ? 我怎样才能做到这一点? Thanks. 谢谢。

string arialuniTff = path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Fonts), "arial.TTF");

Error Msg in 3.5 : 'System.Environment.SpecialFolder' does not contain a definition 3.5中的错误消息:“ System.Environment.SpecialFolder”不包含定义

The fonts folder is typically located at %windir%\\Fonts , so you should be able to get the location like this: 字体文件夹通常位于%windir%\\Fonts ,因此您应该能够获得如下位置:

Path.Combine(
    System.Environment.GetEnvironmentVariable("windir"),
    "Fonts");

It is a virtual folder, so in theory it could be located somewhere else. 它是一个虚拟文件夹,因此从理论上讲它可以位于其他位置。 In practice, I've never seen that happen or heard of it happening. 在实践中,我从未见过或听说过这种情况。 (Microsoft is confident enough in this location to reference it on their " how to install a font " page). (Microsoft在此位置有足够的信心在其“ 如何安装字体 ”页面上引用它)。 I'm sure that if you're trying to locate a specific file name like that you have good error handling already, though. 我敢肯定,如果您试图找到一个像这样的特定文件名,那么您已经具有良好的错误处理能力。

Bonus information: 奖金信息:

You might know this already, but if you need to know what classes, methods, etc. are available in a specific version of the .net framework, you can find out from MSDN. 您可能已经知道这一点,但是如果您需要了解.net框架的特定版本中提供了哪些类,方法等,则可以从MSDN中查找。 Go to the documentation page (say this one on Environment.SpecialFolder ), and click on the ".NET Framework 4.5" link in the top left corner and choose a different version to see the page you are looking at as it was in that version. 转到文档页面(在Environment.SpecialFolder上说这一点),然后单击左上角的“ .NET Framework 4.5”链接,然后选择其他版本以查看该版本的页面。 。

Please refer How to get the path to CSIDL_COMMON_DOCUMENTS in .NET 3.5? 请参阅如何在.NET 3.5中获取CSIDL_COMMON_DOCUMENTS的路径?

It provides the location for const int CSIDL_COMMON_DOCUMENTS = 0x002e; 它提供了const int CSIDL_COMMON_DOCUMENTS = 0x002e;的位置const int CSIDL_COMMON_DOCUMENTS = 0x002e; .

For Fonts folder, use const int CSIDL_FONTS = 0x0014; 对于Fonts文件夹,使用const int CSIDL_FONTS = 0x0014;

It would be: 这将是:

[DllImport("shell32.dll"), CharSet = CharSet.Auto]
static extern int SHGetFolderPath(IntPtr hwndOwner, int nFolder, IntPtr hToken, uint dwFlags, [Out] StringBuilder pszPath);

const int CSIDL_FONTS = 0x0014;
const int CSIDL_FLAG_CREATE = 0x8000;

StringBuilder sb = new StringBuilder();

int retVal = SHGetFolderPath(IntPtr.Zero,
                                 CSIDL_FONTS | CSIDL_FLAG_CREATE,
                                 IntPtr.Zero,
                                 0,
                                 sb);
Debug.Assert(retVal >= 0);  // assert that the function call succeeded
String folderLocation = sb.ToString();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在控制台应用程序中访问文件夹 - Accessing folder in console application 如何在.Net Framework 3.5的WinForms中从控制台应用程序接收输出? - How should I receive output from Console application in WinForms on .Net Framework 3.5? 从控制台应用程序访问虚拟目录 - Accessing Virtual Directory from console application 从控制台应用程序访问SQL Server - Accessing SQL Server from Console application .NET Compact Framework 3.5应用程序中的随机本机异常 - Random native exception from .NET Compact Framework 3.5 application 项目目标框架为3.5时的oData Continuation - oData Continuation when projects target framework is 3.5 如何将 .NET 框架控制台应用程序子文件夹复制到发布文件夹? - How to copy .NET Framework Console Application subfolders to the release folder? 访问资源文件夹.Net 3.5中的文件 - Accessing the Files in the Resource Folder .Net 3.5 在Visual Studio中将控制台应用程序从4.5.1转换为3.5会导致不再拾取控制台App.config - Converting console application from 4.5.1 to 3.5 in visual studio causes the console App.config to no longer be picked up 如何以编程方式将目标框架从项目/解决方案的4.0更改为3.5? - How can I progrommatically change the target framework from 4.0 to 3.5 of a project/solution?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM