如何在 C# 中创建自定义文件扩展名?
我需要有关如何在我的 C# 应用程序中创建自定义文件扩展名的帮助。 我创建了一个基本的笔记管理应用程序。 现在我正在将笔记保存为 .rtf (note1.rtf)。 我希望能够创建一个只有我的应用程序能够理解的文件扩展名(例如,note.not,也许)
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
作为部署点,您应该注意 ClickOnce 支持文件扩展名(只要它不处于“仅在线”模式)。 这使得配置系统以识别新的文件扩展名变得轻而易举。
您可以在项目属性 -> 中找到它 发布-> 选项-> VS2008 中的文件关联。 如果您没有 VS2008,您也可以 执行此操作手动,但这并不有趣。
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.
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.
文件扩展名是您格式的任意选择,它实际上仅取决于您的应用程序在安装时将特定文件扩展名注册为 Windows 中特定类型的文件。
提出您自己的文件格式通常意味着您使用只有您的应用程序可以解析的格式来保存该格式。 它可以是纯文本或二进制,甚至可以使用 XML 或任何格式,重点是您的应用程序应该能够轻松解析它。
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.
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.
您的问题有两种可能的解释:
我的文档的文件格式应该是什么?
您当前正在以 RTF 格式保存笔记。 无论您选择将其保存为什么文件扩展名,任何理解 RTF 格式的应用程序都可以打开您的笔记,只要用户知道它是 RTF 格式并将该应用程序指向该文件。
如果您想以自定义文件格式保存文档,以便其他应用程序无法读取它们。 您需要编写代码来获取由 Rich Edit 控件(我假设您在应用程序中用作编辑器)生成的 RTF 流,并使用您自己的格式将其序列化为二进制流。
我个人认为这不值得付出努力...
我的文档的文件扩展名是什么
您当前正在以 RTF 格式保存文档,文件扩展名为 .rtf。 其他应用程序与该文件扩展名相关联,因此在 Windows 资源管理器中双击此类文件将打开该应用程序,而不是您的应用程序。
如果您希望能够在 Windows 资源管理器中双击文件并打开应用程序,则需要更改正在使用的文件扩展名并为该扩展名创建正确的关联。
文件扩展名关联由注册表中的条目定义。 您可以按计算机(在 HKLM\Software\Classes 中)或按用户(在 HKCU\Software\Classes 中)创建这些,但按计算机是最常见的情况。 有关实际注册表项以及 MSDN 文档和示例的链接的更多详细信息,请查看我对此的回答 有关 Vista 文档图标关联的问题。
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.
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.
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.
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.
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.
我认为这是创建正确的注册表值的问题,
或者查看此 codeproject 的文章
I think it's a matter of create the right registry values,
or check this codeproject's article
您可以使用任何您想要的扩展名来保存文件,只需在保存文件时将其放在文件名中即可。
我感觉您的问题是“我如何以 RTF 以外的方式保存文件?”。 您必须发明自己的格式,但您实际上并不希望这样。 您仍然可以将 RTF 保存到名为 mynote.not 的文件中。
我建议您继续使用可从其他程序读取的格式。 一旦你的用户想要用他们的笔记做一些你的程序不支持的事情,他们会很感激。
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.
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.