3 回答

TA貢獻1799條經驗 獲得超8個贊
這可能是另一種方法,但錯誤似乎是您為每行新行獲得的錯誤。\r\n
我寫了幾行,擺脫了這個錯誤,工作得很好,請閱讀評論并詢問是否有任何不清楚的地方!
/// <summary>
/// Class for a Licence
/// </summary>
class Licence
{
public string sKey { get; set; }
public string sExpiry { get; set; }
public string sName { get; set; }
}
static void Main(string[] args)
{
// Loading the data with a WebClient
WebClient WB = new WebClient();
// Downloads the string
string whitelist = WB.DownloadString("https://pastebin.com/raw/ai8q5GEA");
// List with all licences
List<Licence> licences = new List<Licence>();
// Foreach Row
foreach (string sRow in whitelist.Split('\n'))
{
// Splits the Row
string[] sData = sRow.Split('|');
// Adds the licence data
licences.Add(new Licence
{
sKey = sData[0],
sExpiry = sData[1],
sName = sData[2]
});
}
// User input
string fin = String.Format("2222-2222-2222-2222");
// Gets the licence class for the specific key
Licence lic = licences.Find(o => o.sKey == fin);
// If there a licence found
if (lic != default(Licence))
{
// Uses the licence class for data output
var date = DateTime.ParseExact(lic.sExpiry, "dd-MM-yyyy", System.Globalization.CultureInfo.InvariantCulture);
var result1 = (date - DateTime.Now.Date).Days;
if (result1 >= 0)
{
Console.WriteLine(string.Concat(new string[]
{
"License key is valid.",
Environment.NewLine,
Environment.NewLine,
"Hi ", lic.sName, "! Key expires on the ", lic.sExpiry,
Environment.NewLine,
"Restart DeluxeUnban."
}));
}
}
Console.WriteLine("FINISHED");
Console.ReadKey();
}

TA貢獻1828條經驗 獲得超3個贊
問題是您一次搜索所有行。您需要在換行符上進行拆分并單獨搜索每行。
string data = new System.Net.WebClient() { Proxy = null }.DownloadString("https://pastebin.com/raw/ai8q5GEA");
// Split on line breaks
var lines = data.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
// Search for license key (might want to change this)
var foundLicense = lines.FirstOrDefault(l => l.StartsWith(fin));
if (foundLicense != null)
{
var values = foundLicense.Split('|');
var whitelist = values[0];
var date = DateTime.ParseExact(values[1], "dd-MM-yyyy", System.Globalization.CultureInfo.InvariantCulture);
var username = values[2];
/* ... */
}
就個人而言,我會創建一個類(DTO)來在解析文本時保存信息。
void Main()
{
string fin = "111";
string data = new System.Net.WebClient() { Proxy = null }.DownloadString("https://pastebin.com/raw/ai8q5GEA");
// Parse data to DTO-objects
var licenseData = data.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries)
.Select(l => l.Split('|'))
.Select(x => new LicenseModel
{
LicenseKey = x[0],
Date = DateTime.ParseExact(x[1], "dd-MM-yyyy", System.Globalization.CultureInfo.InvariantCulture),
Username = x[2]
})
.ToList();
// Find matching license
var foundLicense = licenseData.FirstOrDefault(l => l.LicenseKey.StartsWith(fin));
if (foundLicense != null)
{
/* FOUND LICENSE */
}
}
public class LicenseModel
{
public string LicenseKey { get; set; }
public DateTime Date { get; set; }
public string Username { get; set; }
}

TA貢獻1757條經驗 獲得超8個贊
您的白名單包含兩行文本。所以你的第一步應該是按新行拆分它
string listOfUsers = whitelist.Split('\n');
其余的代碼可以放在foreach或for循環中,如下所示:
foreach(var user in ListOfUsers) // thanks to it you will check all users, not only the first one
{
string[] result = user.Split('|');
string whitelistx = result[0];
string expiry = result[1];
string username = result[2];
if (user.Contains(fin))
{
// rest of your code
當然,您可以優化此代碼,但這只是為了回答有關僅檢查第一個用戶的問題。
- 3 回答
- 0 關注
- 118 瀏覽
添加回答
舉報