C#读取资源文件(Resources.resx)中的内容

平时我们可以将字符串、图像、图标、音频、文件等资源存放在resx文件中,vs会自动生成一个强类型资源类Resources,读取资源文件的内容很简单:

Properties.Resources.资源名称

例如:

textBox1.Text = Properties.Resources.string1;
picSearch.Image = Properties.Resources.image1;

或者:

textBox1.Text = Properties.Resources.ResourceManager.GetString("string1", Properties.Resources.Culture);
picSearch.Image = Properties.Resources.ResourceManager.GetObject("image1", Properties.Resources.Culture) as Bitmap;

在Resources.Designer.cs文件中相关定义:

/// <summary>
///   查找字符串。
/// </summary>
internal static string string1 {
    get {
        return ResourceManager.GetString("string1", resourceCulture);
    }
}

/// <summary>
///   查找图像。
/// </summary>
internal static System.Drawing.Bitmap image1 {
    get {
        object obj = ResourceManager.GetObject("image1", resourceCulture);
        return ((System.Drawing.Bitmap)(obj));
    }
}
        
/// <summary>
///   查找图标。
/// </summary>
internal static System.Drawing.Icon ico1 {
    get {
        object obj = ResourceManager.GetObject("ico1", resourceCulture);
        return ((System.Drawing.Icon)(obj));
    }
}
        
/// <summary>
///   查找音频。
/// </summary>
internal static System.IO.UnmanagedMemoryStream sound1 {
    get {
        return ResourceManager.GetStream("sound1", resourceCulture);
    }
}
        
/// <summary>
///   查找文件。
/// </summary>
internal static string file1 {
    get {
        return ResourceManager.GetString("file1", resourceCulture);
    }
}


评论: 0 | 引用: 0 | 查看次数: 15173
发表评论
登录后再发表评论!