0

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

2
  • 2
    So... you wish to know how to copy a file...? Commented Sep 18, 2012 at 22:55
  • No, I need to know how to, by these details, create a file. Because I don't actually have the file physically, only the details of it (which could theoretically create the file)... Commented Sep 19, 2012 at 9:07

2 Answers 2

3

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);
}
Sign up to request clarification or add additional context in comments.

2 Comments

Okay, thank you, I will try this. Will it work for like .png files and .pdf files as well?
As it appears, this snippet works for UTF-8 encoded text files. To write binary content (.png .pdf) use BinaryWriter(), e.g. msdn.microsoft.com/en-us/library/aa720464(v=vs.71).aspx
2

Can you just use the File.Copy method?

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

1 Comment

No, because the file is not parsed from my application, it is sent to me with these details, I don't have the actual file, only these details. Thank's anyways.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.