How can I tell a file what type of data save? (C#) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How can I tell a file what type of data save? (C#)

I mean like .jpg tells that it's a picture or mp3 for audio. I need to create a file extension that save pictures, text and date for my own app

3rd Mar 2018, 5:58 AM
SebGM2018
SebGM2018 - avatar
8 Answers
+ 2
File extensions are just hints. You can use any extension you want. I just recommend staying away from commonly used extensions. The contents/format of the file can be whatever you need it to be as long as you know how to read and write it. For example, you could use a .docx extension on a jpg file. If you click it word will try to open it because the "hint" tells it to use word. If you start Paint and do a File->Open you can browse to it and open it with no problem.
3rd Mar 2018, 7:37 AM
Jesse Bayliss
Jesse Bayliss - avatar
+ 2
In addition to Jesse. File extensions are just a promiss that the file has a certain file format. With a streamwriter you can write a file using (StreamWriter writer = new StreamWriter("important.txt")) { writer.Write("Word "); writer.WriteLine("word 2"); writer.WriteLine("Line"); } https://www.dotnetperls.com/streamwriter Here it is called "important.txt" but if you put in new StreamWriter("important.abc") it wil make a file called "important.abc" for you Windows will ask with which program should open the file because it doesn't have a file-association for it. It does not know the extension. If you have an application that can read the abc-file, you can link that application to the abc-file. In this link streamwriter is used to write a html file. Notice it is a html file because it has the tags in it, we agreed to call html. https://social.msdn.microsoft.com/Forums/windows/en-US/5a13f536-b3ac-4f9f-ac8f-70ee1909d04d/using-streamwriter-to-write-html-file?forum=winforms
3rd Mar 2018, 10:00 PM
sneeze
sneeze - avatar
+ 1
Thanks guys, but any idea of how to use it with save file dialog?
3rd Mar 2018, 10:11 PM
SebGM2018
SebGM2018 - avatar
+ 1
SaveFileDialog dlg = new SaveFileDialog(); dlg.Filter = "Data Files (*.dat)|*.dat"; dlg.DefaultExt = "dat"; dlg.AddExtension = true; https://stackoverflow.com/questions/1213339/save-file-with-appropriate-extension-in-a-save-file-prompt
3rd Mar 2018, 10:14 PM
sneeze
sneeze - avatar
+ 1
yes. using (StreamWriter writer = new StreamWriter("c:\\temp\\important.txt")) { writer.WriteLine("begin"); writer.WriteLine(textBox1.Text); writer.WriteLine(dateTimePicker1.Value); writer.WriteLine("End"); } notice this is very simple and does not use any programming design or data separation. But is does work. Also have al look a the datetimepicker and what date formats it can save, you do not want to analyze date-string to import and export your dates.
3rd Mar 2018, 10:29 PM
sneeze
sneeze - avatar
+ 1
Depends on the target platform. If the target platform is a windows PC And your development enviroment is Visual studio. Visual studio has a build in tool. Which can do that, it is not easy to use but it does the job. You add an installer project to your solution and in this installer project does make the setup.exe. https://www.advancedinstaller.com/user-guide/tutorial-ai-ext-vs.html
4th Mar 2018, 9:49 PM
sneeze
sneeze - avatar
0
Thanks, so now I just tell the app to save the textbox and date picker data no?
3rd Mar 2018, 10:15 PM
SebGM2018
SebGM2018 - avatar
0
Thanks dude, and a last question, for example, I finished my program, and I want to install it and save the files in a folder that the installator creates, how can I do that?
3rd Mar 2018, 10:49 PM
SebGM2018
SebGM2018 - avatar