0

Is there a way to include a File Content inside the Class ,not just Text Files but also Binary Files like a Char Array ,or String or whatever in Earth it could be ,so in Constructor my Class Should write That content into a File in a specified file .

This File can be an .exe , .pdf , .xls , .xml etc...

4 Answers 4

3

I'm not exactly sure what you mean by include "a File Content inside the Class" but from what I'm guessing you mean (storing entire files inside a class?) you may want to look at Base64 encoding which is an encoding scheme that will let you convert any binary data into a ASCII string.

To store your binary data you can convert any byte[] into a Base64 encoded string using Convert.ToBase64String and store that in your class. To get it back use Convert.FromBase64String to get a byte[].

Sign up to request clarification or add additional context in comments.

2 Comments

Storing Entire File inside a class ,that exactly what i mean .
hi cbley that Worked like a Charm ,first i did FileStream stream = new FileStream("Date.exe", FileMode.Open); now i can Store a File as a String inside the class and write it wherever i want to .Best man .
1

Like cbley, I'm not exactly sure what you mean either, but you could mean embedding a file in your assembly?

  • Go into Visual Studio, click "add existing item" and choose your file.
  • Change the Build Action on the file to "Embedded Resource"

When you run your application, you can extract the file binary to the file system like this.

//make sure MyNameSpace.MyFile.xls is the fully qualified name in your assembly to the file
using (Stream stream = Assembly.GetManifestResourceStream("MyNameSpace.Myfile.xls"))
{
   byte[] buf = new byte[stream.Length];
   stream.Read(buf, 0, stream.Length);
   File.WriteAllBytes(@"C:\MyFile.xls", buf);
}

1 Comment

Not as resource because im going to export only a Class File ,not an DLL neither Executable .
0

Have a look at File.ReadAllBytes() method.

Comments

0

That would be possible, just store the file content as byte array. Write a converter which saves the bytes of a specified files into a text file in the format you need (so it represents an array). Then cut and paste this into your source code.

Comments

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.