将windows窗体应用程序转换为web用户控件(功能)

本文关键字:控件 用户 功能 web windows 窗体 应用 程序转换 | 更新日期: 2023-09-27 18:08:49

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

我被要求在我们的服务器上设置这个,这样用户就可以登录到一个网站并运行应用程序。我几乎没有做过web开发,我在网上看到很多答案说你不能将Windows窗体应用程序转换为web窗体应用程序。但这些答案似乎专门指的是转换UI。

我真的只对移植功能感兴趣。我的UI只是一个编辑字符串的文本框、一个显示当前字符串值的列表视图和一个提交更改的按钮。我非常乐意为我的内容设计一个新的UI。但是功能呢?我能把我现在的c#代码和它挂钩到一个web UI吗?或者我需要为web编写不同的代码吗?

除了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窗体应用程序转换为web用户控件(功能)

有一些问题,如果你尝试在Web表单中使用这个代码,第一个是你使用:Environment.CurrentDirectory可能需要使用Server.MapPath代替,另一个是你在代码中直接调用UI,例如MessageBox.ShowListViewItem,连接到UI的代码必须需要一些重新思考,所以你有一个虽然前面的路。

您最好尝试在Web Forms中从头创建应用程序,而不是移植代码,这样可能更容易,并将帮助您更好地理解Web Forms是如何工作的。