Michael
Michael

Reputation: 863

Create file in C# from source code

I'm looking for a way to create a file from a source code (when you open a file in for example notepad it gives you some code, with that code I want to recreate the file, by stating the filetype and filename and then the code that should be in the file.

I hope you can understand my problem, it's a bit hard to explain, I'll go again...

I've got the sourcecode + filename + filetype + file description for the file that I want to create. > Now I want C# to create a file based on that information and make it an exact copy of what that information is taken from. It should save the file, and then the file is to be openable.

Thank you in advance, Mike

Upvotes: 0

Views: 373

Answers (2)

Phillip Ngan
Phillip Ngan

Reputation: 16096

Write "code" into a file mycode.cs

string code = "code";
string filename = "mycode";
string filetype = "cs";
using (FileStream fs = File.Create(filename+"."+filetype))
{
    Byte[] info = new UTF8Encoding(true).GetBytes(code);
    fs.Write(info, 0, info.Length);
}

Upvotes: 3

Abe Miessler
Abe Miessler

Reputation: 85046

Can you just use the File.Copy method?

file.Copy("c:\temp\source.txt", "c:\temp\dest.txt");

Upvotes: 2

Related Questions