how to create a custom file extension in C#? - c#

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)

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.

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.

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.

I think it's a matter of create the right registry values,
or check this codeproject's article

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.

Related

c# development, what type of files store resources (for localization)?

Where Visual Studio 2010 (using c#) stores the resources? I need to monitor the files that stores resources in my product to know when I need a new translation.
I have found this article that talks about it:
http://msdn.microsoft.com/en-us/library/7k989cfy(v=vs.90).aspx
According to that, all the files of type *.resx
I was wondering if there were any exceptions to that. In other words, it is possible that some resources would get stored in files with extension different from *.resx?
Thanks
-Tony
It actually tells you in the first paragraph in the the link you provided
Visual C# applications often include data that is not source code.
Such data is referred to as a project resource and it can include
binary data, text files, audio or video files, string tables, icons,
images, XML files, or any other type of data that your application
requires. Project resource data is stored in XML format in the .resx
file (named Resources.resx by default) which can be opened in Solution
Explorer.
If you want to be sure you can always right-click on the file and select open with. This lets you choose a different editor to view the file in.
If you follow recommended practice - yes all text resources are stored in resx files.
There are also other types of resources that may need to be localized - i.e. images may contain text/language specific content.
Note that if code does not strictly follow recommended practice some text that must be localized ends up in source code (i.e. format strings for String.Format calls).

Reading an STG file created from MS ActiveSync

How can I parse an STG (Microsoft ActiveSync Mobile Device Backup) file (created by ActiveSync)?
I have an old .stg file that was created from backing up an old Windows Mobile device, and I would like to write a program to read it using C#. I have tried a few things but can't get anything but garbage when I read through it. From what I can find online it is stored in Unicode format but that's about it, any posts I find talking about it are ancient and all the links are dead.
You can open ActiveSync .stg files in an archiver tool such as 7-Zip or PeaZip. If the program does not recognize the file as an archive, you may need to rename the file to have an archive extension (e.g., .7z, .gz, .tar).
With PeaZip is also possible to try to open any file extension from "Open as archive" menu entry (also available in system's context menu), without changing the file's extension.
With many format being basically variations of Deflate-compressed containers, it is worth trying.
Anyway, some formats may introduce proprietary fields (e.g. extra checksums, comments, digital signing, etc) which are out of the scope for a general-purpose archiver as PeaZip or 7-Zip.
So, while it is harmless to try to read container files as archives, it is not adviceable to try to edit them as such.

.NET Write a file with File Version Information attached

Is there anyway to say... Include a version number when creating a text file?
Basically, my process is writing a text file that I need to check if there's a newer version available. My plan was to use FileVersionInfo to determine the current version and the version on the PC. However, I can't figure out how to write the file to the PC with a version attached to the file.
Any ideas?
Typical options here include;
hashing the contents and comparing that
relying onthe audit dates
storing version in the file and
storing version in the first line of the file
using file-watcher events (unreliable by itself, by most accounts)
using the alternative data streams in NTFS
But no; plain text files don't have much associated metadata by themselves
Plain text files have no embedded resources (except for file attributes such as the file date).
Either write the version as part of the text that can easily be parsed, use the file date to track versions, or use binary files and embed your own resources.

Is it possible to add custom metadata to file?

I know that each file has metadata like title, subject, keywords and comments:
But what if I need custom metadata like tags for example?
Is it possible to do it with C#?
I know that each file has metadata like title, subject, keywords and comments
That is not true. Most file types do not have a 'standard' form of metadata.
In particular, PDF files don't have properties that Windows Explorer recognizes.
Metadata (file attributes) is not a function of the filesystem.
Office files use a structured format that allows for such attributes.
Jpeg ues EXIF, a different format
If using NTFS you can store whatever you like in an Alternate data stream
As per Jesper's comment, you can use the DSOFile library to read and write to Custom properties that are stored in ADS.
Works well for me though note the fact that the properties are lost when file is transferred to a different file system, including email.
see http://www.keysolutions.com/blogs/kenyee.nsf/d6plinks/KKYE-79KRU6 for a 64bit implementation, link to MS original and comments.
This will depend on whether the file type you are working with supports this. For example this will not be possible with a text file.
Has anyone thought of using the File ID for this? You can get it via the command
fsutil file queryfileid "C:/my/path/example.txt"
This could be used to store information about this file in a separate storage-file associated with the id.

Getting the type of file from the ASP.NET FileUpload control?

I want to get the type of file uploaded using the ASP.NET FileUpload control. When I upload a file, I want to be able to get the type of file uploaded, so I can assign a an icon to the file (such as a word, excel, pdf icon).
Here is the problem, I can't go off the file extension because a file could be called test.xxxxxxxx and be a valid pdf file, or a file might not have an extension.
The other option is to read the content-type, but with some of these appear not to be standard or in a simple to read format such as excel files, so is there another option to determine the file type?
I would review the process that names a PDF file "test.xxxxxxxx" without a ".pdf" extension. Most workflows that files have some kind of naming convention (manual or automated)
If you cannot read the extension, the file format will need to be detected by interogating markers that make it a known file format: eg: if the stream starts with or contains "%PDF" its a PDF.
If you don't know the extension, then you would have to know the file format of popular file types and then read the file and see if it matches one of your known formats. That doesn't sound optimal, though.
An easy way to check the true type of a file server side, using System.IO.BinaryReader, is described here:
http://forums.asp.net/post/2680667.aspx
and VB version here:
http://forums.asp.net/post/2681036.aspx
You'll need to know the binary 'codes' for the file type(s) you're checking for, but you can get those by implementing this solution and debugging the code.
Also note that when the BinaryReader is closed with the r.Close() statement, this will make FileUploader.HasFile = False

Categories