Steps:
1. Create a windows application with one button and name it as Embeded.
2. Add some existing files like image,Text file,etc..to your application by Right click on your file name which is in solution explorer and then click add - > Existing Item - > choose any file you want.
3. Then go to the properties of the files which you added then change its Build Action property to Embedded Resource.
4. Double click the Embeded button and write the following code
List<string> lstFileName = new List<string>();
try
{
string s = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
int p = s.IndexOf("\\bin");
string t = s.Substring(0, p);
DirectoryInfo sourcedinfo = new DirectoryInfo(t);
foreach (FileInfo fi in sourcedinfo.GetFiles())
{
if (fi.Extension != ".cs" && fi.Extension != ".resx" && fi.Extension != ".csproj" && fi.Extension != ".user")
{
lstFileName.Add(fi.Name);
}
}
}
catch (Exception ex)
{
MessageBox.Show("Error while reading file from the path");
}
for (int i = 0; i < lstFileName.Count; i++)
{
GetResource(lstFileName[i].ToString());
}
MessageBox.Show("Successfully Added embeded files to Documents");
5.Then add the below function
public void GetResource(string filename)
{
string s = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
string p = Path.GetFileNameWithoutExtension(s);
string name = p.Substring(0, p.LastIndexOf("."));
System.Reflection.Assembly asm = System.Reflection.Assembly.GetExecutingAssembly();
string val = "";
System.IO.Stream strm = asm.GetManifestResourceStream("WindowsFormsApplication1." + filename);
if (strm != null)
{
byte[] coffImage = new byte[strm.Length];
strm.Read(coffImage, 0, coffImage.Length);
System.IO.StreamReader reader = new System.IO.StreamReader(strm);
val = reader.ReadToEnd();
reader.Close();
string addinDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\" + name;
if (Directory.Exists(addinDirectory) == false)
{
Directory.CreateDirectory(addinDirectory);
}
System.IO.File.WriteAllBytes(addinDirectory + @"\" + filename, coffImage);
}
}
6.Run your program and then click Embedded button all the embedded files will be pasted in documents.
Thanks for reading this article