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

时间:2022-04-22 05:06:56

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,也许)

5 个解决方案

#1


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或任何格式,重点是您的应用程序应该能够轻松解析它。

#2


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,你也可以手动完成,但这并不好玩。

#3


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问题的答案。

#4


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

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

or check this codeproject's article

或者查看这个codeproject的文章

#5


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.

我建议你继续使用其他程序可读的格式。一旦他们想要对您的程序不支持的笔记执行某些操作,您的用户将会感激不尽。

#1


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或任何格式,重点是您的应用程序应该能够轻松解析它。

#2


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,你也可以手动完成,但这并不好玩。

#3


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问题的答案。

#4


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

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

or check this codeproject's article

或者查看这个codeproject的文章

#5


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.

我建议你继续使用其他程序可读的格式。一旦他们想要对您的程序不支持的笔记执行某些操作,您的用户将会感激不尽。