【问题标题】:What's the .Net replacement for File.Type FileSystemObject property?File.Type FileSystemObject 属性的 .Net 替代品是什么?
【发布时间】:2009-10-17 20:42:26
【问题描述】:

在经典的 Asp 中,我们使用 File.Type 属性从注册表中获取与文件类型关联的友好名称(例如,“.txt”的“文本文档”)。通常替换旧 COM 对象的 FileInfo 类没有复制此功能,到目前为止,我在寻找替代品方面没有取得太大成功。

【问题讨论】:

标签: .net filesystemobject


【解决方案1】:

我不知道 BCL 中有一个方法,但您可以从注册表中轻松读取它:

using System;
using Microsoft.Win32;

class Program
{
    static void Main(string[] args)
    {

        string extension = ".txt";
        string nicename = "";

        using (RegistryKey key = Registry.ClassesRoot.OpenSubKey(extension))
        {
            if (key != null)
            {
                string filetype = key.GetValue(null) as string;
                using (RegistryKey keyType = Registry.ClassesRoot.OpenSubKey(filetype))
                {
                    if (keyType != null)
                    {
                        nicename = keyType.GetValue(null) as string;
                    }
                }
            }
        }
        Console.WriteLine(nicename);

    }
}

但是,Vladimir 提供的link 中使用的方法是首选,因为它使用 API 接口。

【讨论】:

  • 我希望避免手动读取这些值,但考虑到选项我可能会走这条路
猜你喜欢
  • 2017-01-24
  • 1970-01-01
  • 2011-08-31
  • 2014-04-12
  • 2018-11-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-22
相关资源
最近更新 更多