Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello...
My question is, when my app is installing i wanna a create a file to a specified location. So my idea is to put the file into solution explorer and read the content to programtically created file. But I'm unable to access the file from solution explorer
Can u help me?
Posted

You can add the file as a resource to your project. See here[^]. :)

Also if you plan to make a Setup and Deployment project for your application you can add your files in this project and copy them during installation.

[UPDATE]
Check out this[^] MSDN article. It demonstrates both how to embed files and how to access files from resources.

Here is a quick demo using the technique described in the article.
I've created a new windows forms application and added a "Snake.exe" file to this application. After that I changed the Build Action property (from properties of the newly added resource) to Embed Resource (it is described in the article). And here is the code for accessing the resource and writing it to file:
C#
// Demo....
try
{
    Assembly assembly = Assembly.GetExecutingAssembly();   
    Stream stream = assembly.GetManifestResourceStream("MyNameSpace.Resources.Snake.exe");
    if(stream != null)
    {
        // Write the stream content to file
        using (FileStream fileStream = new FileStream("C:\\Snake.exe", FileMode.Create))
        {
            using (BinaryWriter bw = new BinaryWriter(fileStream))
            {
                byte[] buffer = ReadAllBytes(stream);
                bw.Write(buffer);
                bw.Close();
            }
        }
    }
}
catch
{
    MessageBox.Show("Error accessing resources!");
}


In the above code I call a helper method (ReadAllBytes) to read the stream content to a byte buffer. Here is the code for this method:
C#
public static byte[] ReadAllBytes(Stream input)
{
    byte[] buffer = new byte[1024 * 8];
    using (MemoryStream ms = new MemoryStream())
    {
        int read;
        while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
        {
            ms.Write(buffer, 0, read);
        }
        return ms.ToArray();
    }
}


I hope this demo is useful for you. If you encounter some problems, you can always check the embedded resource names with Assembly.GetManifestResourceNames[^] or using ILDASM. :)
[/UPADTE]
 
Share this answer
 
v5
Comments
krishna kishore58 11-Feb-11 6:30am    
yup thats cool but my file is dat file then how can i access it...
Nuri Ismail 11-Feb-11 7:23am    
See my updated answer.
krishna kishore58 11-Feb-11 7:26am    
well i got it I'm able to access the file from solution explorer. But please answer to my question is "Is it good programming to put the file IS.DAT(159 KB) into solution explorer so i need to create IS.DAT file at desired location when required. Because i donto want to carry th IS.DAT file when my App goes to many clients"
Please answer....
Nuri Ismail 11-Feb-11 7:35am    
Your file is not a big one and I see no problem embedding this file as a resource.
As I told you in my original answer - if you plan to create a Setup and Deployment project for your application then it is better to include this file there.
krishna kishore58 11-Feb-11 7:28am    
But how do u added the file to resources?
You can add any file to your project, not only resource or code file — any file.
You can also indicate if the file it to be copied to output or not, only remember it will be copied according to the project's file structure.

You can add any existing file (Add item => Existing file) from any location; remember, in this case it will be copied to the location in your project according to its directory structure. Another (very useful) option is to add a reference (option if file dialog box). In this case, the file is not copied.

Finally, important to know: if you add new folder, it created physically on the file structure. If you added, it is not removed from file structure, only from the project. (I don't know why, but it will prevent you from adding same folder again.) In contrast, the file is added and removed from file structure as well.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900