如何在 C# 中创建自定义文件扩展名?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/780319/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 00:52:39  来源:igfitidea点击:

how to create a custom file extension in C#?

c#file-extension

提问by MarlonRibunal

I need help in how to create a custom file extension in my C# app. I created a basic notes management app. Right now I'm saving my notes as .rtf (note1.rtf). I want to be able to create a file extension that only my app understands (like, note.not, maybe)

我需要有关如何在我的 C# 应用程序中创建自定义文件扩展名的帮助。我创建了一个基本的笔记管理应用程序。现在我将我的笔记保存为 .rtf (note1.rtf)。我希望能够创建一个只有我的应用程序才能理解的文件扩展名(例如,note.not,也许)

采纳答案by Jon Limjap

File extensions are an arbitrary choice for your formats, and it's only really dependent on your application registering a certain file extension as a file of a certain type in Windows, upon installation.

文件扩展名是您的格式的任意选择,它只真正依赖于您的应用程序在安装时将某个文件扩展名注册为 Windows 中的某种类型的文件。

Coming up with your own file format usually means you save that format using a format that only your application can parse. It can either be in plain text or binary, and it can even use XML or whatever format, the point is your app should be able to parse it easily.

提出您自己的文件格式通常意味着您使用只有您的应用程序可以解析的格式来保存该格式。它可以是纯文本或二进制,甚至可以使用 XML 或任何格式,关键是您的应用程序应该能够轻松解析它。

回答by Jhonny D. Cano -Leftware-

I think it's a matter of create the right registry values,

我认为这是创建正确的注册表值的问题,

or check this codeproject's article

或查看此代码项目的文章

回答by Srdjan Jovcic

You can save file with whatever extension you want, just put it in file name when saving file.

您可以使用您想要的任何扩展名保存文件,只需在保存文件时将其放在文件名中即可。

I sense that your problem is "How I can save file in something other than RTF?". You'll have to invent your own format, but you actually do not want that. You still can save RTF into file named mynote.not.

我感觉您的问题是“我如何将文件保存为 RTF 以外的格式?”。您将不得不发明自己的格式,但实际上您并不想要那样。您仍然可以将 RTF 保存到名为 mynote.not 的文件中。

I would advise you to keep using format which is readable from other programs. Your users will be thankful once they want to do something with their notes which is not supported by your program.

我建议您继续使用其他程序可读的格式。一旦你的用户想用他们的笔记做一些你的程序不支持的事情,他们就会很感激。

回答by Franci Penov

There are two possible interpretations of your question:

你的问题有两种可能的解释:

What should be the file format of my documents?

我的文档的文件格式应该是什么?

You are saving currently your notes in the RTF format. No matter what file name extension you choose to save them as, any application that understands the RTF format will be able to open your notes, as long as the user knows that it's in RTF and points that app to that file.

您当前正在以 RTF 格式保存您的笔记。无论您选择将它们保存为什么文件扩展名,只要用户知道它是 RTF 格式并将该应用程序指向该文件,任何理解 RTF 格式的应用程序都可以打开您的笔记。

If you want to save your documents in a custom file format, so that other applications cannot read them. you need to come up with code that takes the RTF stream produced by the Rich Edit control (I assume that's what you use as editor in your app) and serializes it in a binary stream using your own format.

如果您想以自定义文件格式保存您的文档,以便其他应用程序无法读取它们。您需要想出一些代码,该代码采用 Rich Edit 控件生成的 RTF 流(我假设这就是您在应用程序中用作编辑器的内容)并使用您自己的格式将其序列化为二进制流。

I personally would not consider this worth the effort...

我个人认为这不值得付出努力......

What is the file name extension of my documents

我的文档的文件扩展名是什么

You are currently saving your documents in RTF format with .rtf file name extension. Other applications are associated with that file extension, so double-clicking on such file in Windows Explorer opens that application instead of your.

您当前正在以 RTF 格式保存文档,文件扩展名为 .rtf。其他应用程序与该文件扩展名相关联,因此在 Windows 资源管理器中双击此类文件会打开该应用程序而不是您的。

If you want to be able to double click your file in Windows Explorer and open your app, you need to change the file name extension you are using AND create the proper association for that extension.

如果您希望能够在 Windows 资源管理器中双击您的文件并打开您的应用程序,您需要更改您正在使用的文件扩展名并为该扩展名创建正确的关联。

The file extension associations are defined by entries in the registry. You can create these per-machine (in HKLM\Software\Classes) or per-user (in HKCU\Software\Classes), though per-machine is the most common case. For more details about the actual registry entries and links to MSDN documentation and samples, check my answer to this SO question on Vista document icon associations.

文件扩展名关联由注册表中的条目定义。您可以为每台机器(在 HKLM\Software\Classes 中)或每用户(在 HKCU\Software\Classes 中)创建这些,尽管每台机器是最常见的情况。有关实际注册表项以及指向 MSDN 文档和示例的链接的更多详细信息,请查看我在 Vista 文档图标关联上对此SO 问题的回答。

回答by Marc Gravell

As a deployment point, you should note that ClickOnce supports file extensions (as long as it isn't in "online only" mode). This makes it a breeze to configure the system to recognise new file extensions.

作为部署点,您应该注意 ClickOnce 支持文件扩展名(只要它不处于“仅在线”模式)。这使得配置系统以识别新文件扩展名变得轻而易举。

You can find this in project properties -> Publish -> Options -> File Associations in VS2008. If you don't have VS2008 you can also do it manually, but it isn't fun.

您可以在 VS2008 中的项目属性 -> 发布 -> 选项 -> 文件关联中找到它。如果您没有 VS2008,您也可以手动进行,但并不好玩。