[英]Convert windows forms app to web user control (functionality)
我有一個用於編輯配置文件的C#Windows窗體應用程序。 它基本上從XML文件中讀取字符串,並允許管理員根據需要編輯該字符串。
我被要求在我們的服務器上進行設置,以便用戶可以登錄網站並運行該應用程序。 我幾乎沒有做過任何網絡開發工作,並且在線上看到很多答案,它們說您不能將Windows Forms App轉換為Web Forms應用程序。 但是這些答案似乎專門針對轉換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;
如果您嘗試在Web窗體中使用此代碼,則會遇到一些問題,第一個是您使用的: Environment.CurrentDirectory
可能需要使用Server.MapPath
來代替,另一個是您可以直接在其中調用UI您的代碼,例如MessageBox.Show
和ListViewItem
,連接到UI的代碼必須進行一些重新思考,因此您的工作尚需時日。
您最好嘗試在Web窗體中從頭開始重新創建應用程序,而不是移植代碼,這樣做可能更容易,並且將幫助您更好地了解Web窗體的工作原理。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.