简体   繁体   中英

Convert windows forms app to web user control (functionality)

I have a C# Windows Forms Application that edits a config file. It basically reads a string from an XML file, and allows an admin to edit that string if it needs.

I've been asked to set this up on our server, so that users can log into a website and run the application. I've done next to no web development, and I'm seeing a lot of answers online that say you can't convert a Windows Forms App to a web forms app. But those answers appear to refer specifically to converting the UI.

I'm really only interested in porting the functionality. My UI is just a text box to edit the string, a list view that shows values from the current string, and a button to submit changes. I'm more than happy to design a new UI for my content. But how about the functionality? Can I take my current C# code and just hook it into a web UI? Or do I need to code differently for the web?

Aside from the button_Click and KeyDown functions, I really just have these two:

List what's in the string:

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

Add to the string:

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

My using statements, in case they shed light on anything:

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;

There are a few problems if you try to use this code in Web Forms, the first one is you use of: Environment.CurrentDirectory which may need to use Server.MapPath instead, the other one is that you have calls to the UI directly in your code, for example MessageBox.Show and ListViewItem , the code that hooks up into the UI must require some rethinking, so you have a though road ahead.

You may better try to recreate the application from scratch in Web Forms rather than porting the code, it may be easier that way and will help you to better understand how Web Forms works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM