将消息框列表添加到c#中的资源文件中

时间:2013-01-16 14:55:24

标签: c#

我已经使用try catch和extra消息框编写了我的代码,但现在我必须将消息框放入资源文件中,我该怎么做?

这是我的代码:

  public void btnUpload_Click(object sender, EventArgs e)
    {
        try
        {
            // in the filepath variable we are going to put the path file that we browsed.
            filepath = txtPath.Text;
            if (filepath == string.Empty)
            {
                MessageBox.Show("No file selected. Click browse and select your designated file.");
            }
       }

2 个答案:

答案 0 :(得分:1)

您可以使用设计器(Resources.resx)将这些消息作为String添加到主应用程序资源文件中,然后使用Properties命名空间访问它们。假设你添加这个:

ErrorNoFile | "No file selected. Click browse and select your designated file."

您可以这样称呼它:

MessageBox.Show(Properties.Resources.ErrorNoFile);

如果您修改资源文件中的条目名称,它将自动重构,至少使用我正在使用的VS2012。如果你想将这些消息保存在一个单独的资源中,那么只有ResourceManager才会有效,否则对我来说这似乎有点过分了。

答案 1 :(得分:0)

// Create a resource manager to retrieve resources.
ResourceManager rm = new ResourceManager("items", Assembly.GetExecutingAssembly());

// Retrieve the value of the string resource named "filepath".
// The resource manager will retrieve the value of the  
// localized resource using the caller's current culture setting.

public void btnUpload_Click(object sender, EventArgs e)
    {
        try
        {
            // in the filepath variable we are going to put the path file that we browsed.
            filepath = txtPath.Text;
            if (filepath == string.Empty)
            {
                String str = rm.GetString("welcome");
                MessageBox.Show(str);
            }
       }