|
Hi All, I've seen the reading of text from and writing of text to a file using C# StreamReader and StreamWriter. Also the file created is of type text. I was wondering if I can create various file types like CSS, HTML, JavaScript, etc simply by giving each file name an appropriate extension it requires. I don't have visual studio on hand so I can not test my idea. Thank you in advance for your reply.
modified 22-Jan-13 23:54pm.
|
|
|
|
|
You can create almost any type of file using StreamReader and StreamWriter .
|
|
|
|
|
Giving the file a valid extension will create that file. But the real question would be contents of the file. If you put valid html in a .html file than it is all fine but the moment you go ahead and start creating .exe files you will have to think how valid it is since you can not put a valid binary data in it.
So the answer to your question is yes but be careful about the file type you create.
Twenty years from now you will be more disappointed by the things that you didn't do than by the ones you did do. So throw off the bowlines. Sail away from the safe harbor. Catch the trade winds in your sails. Explore, Dream. Discover.
|
|
|
|
|
You can use a StreamWriter to create any type of file - you are not limited to text:
using (StreamWriter writer = new StreamWriter(@"D:\Temp\MyFile.html"))
{
writer.Write(textToAdd);
} Will work fine, and will create a file with the HTML extension. However, that doesn't mean that the file will be readable by a web browser if you double click it - it depends on the content. Similarly, if you use a file name with a .DLL extension, it will be created, but it won;t work if you try to add it as a reference unless you write suitable content into it, or .AVI but it won't show a movie.
The extension just controls which application(s) will try to open the file - the content must also be correct and applicable to the application. You can open a .DLL file in VLC Media player provided the content is a video file and not an executable program!
If you get an email telling you that you can catch Swine Flu from tinned pork then just delete it. It's Spam.
|
|
|
|