【问题标题】:Convert windows forms app to web user control (functionality)将 Windows 窗体应用程序转换为 Web 用户控件(功能)
【发布时间】:2013-04-13 06:29:17
【问题描述】:

我有一个编辑配置文件的 C# Windows 窗体应用程序。它基本上从 XML 文件中读取字符串,并允许管理员在需要时编辑该字符串。

我被要求在我们的服务器上进行设置,以便用户可以登录网站并运行应用程序。我几乎没有做过 Web 开发,而且我在网上看到很多答案说您无法将 Windows 窗体应用程序转换为 Web 窗体应用程序。但这些答案似乎专门指转换 UI。

我真的只对移植功能感兴趣。我的 UI 只是一个用于编辑字符串的文本框、一个显示当前字符串值的列表视图以及一个用于提交更改的按钮。我非常乐意为我的内容设计一个新的 UI。但是功能呢?我可以使用我当前的 C# 代码并将其挂接到 Web UI 中吗?还是我需要为网络编写不同的代码?

除了 button_Click 和 KeyDown 函数,我真的只有这两个:

列出字符串中的内容:

    /// <summary>
    /// Find current phrases being ignored and list them
    /// </summary>
    internal void listPhrases()
    {
        string myFile = Environment.CurrentDirectory + "\\CGEmailCnctr.exe.config";

        string strFile = File.ReadAllText(myFile);
        var sb = new StringBuilder(strFile);

        string getThis = "<add key=\"messageFilter\" value=\"";
        string subStr = strFile.Substring(strFile.IndexOf(getThis) + getThis.Length);
        string[] igPhrases = subStr.Substring(0, subStr.IndexOf(";\"")).Split(';');

        foreach (string s in igPhrases)
        {
            listView1.Items.Add(s);
        }
    }

添加到字符串中:

    /// <summary>
    /// Checks to see if the phrase is already in the list.  If not, then add it.
    /// </summary>
    /// <param name="addThis"></param>
    internal void addPhrase(string addThis)
    {
        foreach (ListViewItem lvi in listView1.Items)
        {
            if (addThis == lvi.Text)
            {
                lvi.Selected = true;
                MessageBox.Show("List of phrases already contains \"" + addThis + ".\"  Please check the phrase again. \nIf the problem persists, contact your system administrator.");
                return;
            }
        }

        string myFile = Environment.CurrentDirectory + "\\CGEmailCnctr.exe.config";
        string pattern = "messageFilter";
        string pattern2 = ";\"";
        string igPhrase = ";" + addThis + ";\"";

        string strFile = File.ReadAllText(myFile);
        var sb = new StringBuilder(strFile);

        //Find messageFilter, the key we need to change, and get the index of it
        int index = strFile.IndexOf(pattern);
        string after = strFile.Substring(index);

        strFile = strFile.Substring(0, index) + strFile.Substring(index).Replace(pattern2, igPhrase);

        //MessageBox.Show(strFile);

        try
        {
            File.WriteAllText(myFile, strFile);

            MessageBox.Show("Operation complete.  Messages with the phrase \"" + addThis + "\" in the subject line will no longer automatically generate ChangeGear tickets.", "Phrase Added");

            listView1.Items.Add(addThis);
        }
        catch (Exception ex)
        {
            MessageBox.Show("Operation failed. \n" + ex.ToString());
        }
    }

我的 using 语句,以防它们说明任何事情:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

【问题讨论】:

  • 查看“单击一次”应用程序,这些应用程序是可以从服务器运行的 Windows 窗体应用程序

标签: c# web-applications


【解决方案1】:

如果您尝试在 Web 窗体中使用此代码会出现一些问题,第一个问题是您使用的是:Environment.CurrentDirectory,可能需要使用 Server.MapPath,另一个是您调用了UI 直接在您的代码中,例如 MessageBox.ShowListViewItem,与 UI 挂钩的代码必须需要重新考虑,因此您有一条前进的道路。

您最好尝试在 Web 窗体中从头开始重新创建应用程序,而不是移植代码,这样可能更容易,并且将帮助您更好地了解 Web 窗体的工作原理。

【讨论】:

  • 当然,如果您赶时间,和​​/或应用程序需要修改客户端本地计算机中的文件,点击一次可能会对您有所帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-09-24
  • 1970-01-01
  • 1970-01-01
  • 2023-04-11
相关资源
最近更新 更多