从解决方案资源管理器Win Forms访问文件 [英] Acess a file from solution explorer Win Forms

查看:78
本文介绍了从解决方案资源管理器Win Forms访问文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好...
我的问题是,当我的应用程序正在安装时,我想创建一个文件到指定位置.因此,我的想法是将文件放入解决方案资源管理器并读取内容,以编程方式创建文件.但是我无法从解决方案资源管理器访问文件
您可以帮我吗?

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?

推荐答案

您可以将文件作为资源添加到项目中.请参见此处 [ [文件.之后,我将构建操作"属性(从新添加的资源的属性)更改为嵌入资源"(在本文中进行了介绍).这是用于访问资源并将其写入文件的代码:
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:
// 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!");
}



在上面的代码中,我调用了一个辅助方法(ReadAllBytes)来将流内容读取到字节缓冲区.这是此方法的代码:



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:

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();
    }
}



我希望这个演示对您有用.如果遇到一些问题,可以始终使用
Assembly.GetManifestResourceNames检查嵌入的资源名称. [ ^ ]或使用 ILDASM . :)
[/UPADTE]



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]


您可以将任何文件添加到您的项目中,而不仅仅是资源或代码文件-任何文件. 您还可以指示是否将其复制到输出文件,仅记住它将根据项目的文件结构进行复制.

您可以从任何位置添加任何现有文件(添加项目=>现有文件);请记住,在这种情况下,它将根据其目录结构复制到项目中的位置.另一个(非常有用的)选项是添加引用(如果是文件对话框,则为选项).在这种情况下,不会复制文件.

最后,重要的一点是要知道:如果添加新文件夹,它实际上是在文件结构上创建的.如果添加,则不会从文件结构中删除它,而只会从项目中删除它. (我不知道为什么,但是它会阻止您再次添加相同的文件夹.)相反,该文件也会从文件结构中添加和删除.

—SA
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


这篇关于从解决方案资源管理器Win Forms访问文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆