📄 profilelist.cs
字号:
using System;
using System.Text;
using System.Collections;
using System.Xml;
namespace Profile
{
public class ProfileList
{
ArrayList _lstProfile;
public ProfileList()
{
_lstProfile = new ArrayList();
}
public ArrayList List
{
get { return _lstProfile; }
set { _lstProfile = value; }
}
public void SaveFile(string strPath)
{
XmlDocument xmlDoc = new XmlDocument();
XmlDeclaration xmlDeclaration = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", null);
XmlElement rootNode = xmlDoc.CreateElement("ProfileList");
xmlDoc.InsertBefore(xmlDeclaration, xmlDoc.DocumentElement);
xmlDoc.AppendChild(rootNode);
// Create a new <Category> element and add it to the root node
XmlElement parentNode;
IEnumerator ie = _lstProfile.GetEnumerator();
Profile p;
while (ie.MoveNext())
{
p = (Profile)ie.Current;
parentNode = xmlDoc.CreateElement("Profile");
parentNode.SetAttribute("Name", p.ProfileName);
parentNode.SetAttribute("Device", p.DeviceName);
parentNode.SetAttribute("Address", p.Address);
parentNode.SetAttribute("Login", p.Login);
parentNode.SetAttribute("Password", p.Password);
xmlDoc.DocumentElement.AppendChild(parentNode);
}
xmlDoc.Save(strPath);
}
public void LoadFile(string strPath)
{
_lstProfile = new ArrayList();
XmlDocument doc = new XmlDocument();
try
{
doc.Load(strPath);
XmlNodeReader reader = new XmlNodeReader(doc);
while (reader.Read())
{
switch (reader.NodeType)
{
case XmlNodeType.Element:
if (reader.Name.Equals("Profile"))
{
string strName = "";
string strAddr = "";
string strDevice = "";
string strLogin = "";
string strPassword = "";
for (int i = 0; i < reader.AttributeCount; i++)
{
reader.MoveToAttribute(i);
if (reader.Name == "Name") strName = reader.Value;
if (reader.Name == "Device") strDevice = reader.Value;
if (reader.Name == "Address") strAddr = reader.Value;
if (reader.Name == "Login") strLogin = reader.Value;
if (reader.Name == "Password") strPassword = reader.Value;
}
_lstProfile.Add(new Profile(strName, strAddr, strDevice, strLogin, strPassword));
}
break;
default:
break;
}
}
}
catch
{
SaveFile(strPath);
}
}
public void UpdateFile(string strPath)
{
}
public void AddProfile(Profile _profile)
{
_lstProfile.Add(_profile);
}
public void AddProfile(string strPro, string strAddress, string strDevice, string strLogin, string strPass)
{
Profile _profile = new Profile(strPro, strAddress, strDevice, strLogin, strPass);
_lstProfile.Add(_profile);
}
public void Remove(string strPro)
{
IEnumerator ie = _lstProfile.GetEnumerator();
Profile p;
while (ie.MoveNext())
{
p = (Profile)ie.Current;
if (p.ProfileName.Equals(strPro))
{
_lstProfile.Remove(p);
break;
}
}
}
public void Modify(string strPro, string strAddress, string strDevice, string strLogin, string strPass)
{
IEnumerator ie = _lstProfile.GetEnumerator();
Profile p;
while (ie.MoveNext())
{
p = (Profile)ie.Current;
if (p.ProfileName.Equals(strPro))
{
_lstProfile.Remove(p);
Profile newPro = new Profile(strPro, strAddress, strDevice, strLogin, strPass);
_lstProfile.Add(newPro);
break;
}
}
}
public bool IsProfileExists(string strPro)
{
bool IsExists = false;
IEnumerator ie = _lstProfile.GetEnumerator();
Profile p;
while (ie.MoveNext())
{
p = (Profile)ie.Current;
if (p.ProfileName.Equals(strPro))
{
IsExists = true;
break;
}
}
return IsExists;
}
public Profile GetProfile(string strPro)
{
IEnumerator ie = _lstProfile.GetEnumerator();
Profile p = new Profile();
while (ie.MoveNext())
{
p = (Profile)ie.Current;
if (p.ProfileName.Equals(strPro))
{
break;
}
}
return p;
}
/*
~ProfileList()
{
Dispose(false);
}
public void Dispose()
{
Dispose(true);
System.GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
_lstProfile.Clear();
}
}
*/
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -