Files
shack2 cee66afc67 update20190830
update20190830
2019-08-30 14:26:37 +08:00

346 lines
11 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using SuperSQLInjection.model;
namespace tools
{
class FileTool
{
public static List<string> readAllDic(String dic)
{
List<string> fs = new List<string>();
try
{
DirectoryInfo din = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory+"/"+dic);
FileInfo[] files = din.GetFiles();
foreach (FileInfo f in files)
{
fs.Add(f.Name);
}
}
catch (Exception re)
{
Tools.SysLog(dic + "读取错误!" + re.Message);
}
return fs;
}
public static List<String> readFileToList(String path)
{
List<String> list = new List<String>();
FileStream fs_dir = null;
StreamReader reader = null;
try
{
fs_dir = new FileStream(AppDomain.CurrentDomain.BaseDirectory + "/" + path, FileMode.Open, FileAccess.Read);
reader = new StreamReader(fs_dir);
String lineStr;
while ((lineStr = reader.ReadLine()) != null)
{
if (!lineStr.Equals(""))
{
list.Add(lineStr);
}
}
} catch (Exception e)
{
Tools.SysLog(e.Message);
}
finally {
if (reader != null)
{
reader.Close();
}
if (fs_dir != null)
{
fs_dir.Close();
}
}
return list;
}
public static HashSet<String> readDomainToList(String path,Boolean isCleanExists)
{
HashSet<String> list = new HashSet<String>();
FileStream fs_dir = null;
StreamReader reader = null;
try
{
fs_dir = new FileStream(path, FileMode.Open, FileAccess.Read);
reader = new StreamReader(fs_dir);
String lineStr;
while ((lineStr = reader.ReadLine()) != null)
{
if (!lineStr.Equals("")&&!lineStr.StartsWith("http")) {
lineStr = "http://" + lineStr;
}
if (list.Contains(lineStr) && isCleanExists) {
continue;
}
list.Add(lineStr);
}
}
catch (Exception e)
{
Tools.SysLog(e.Message);
}
finally
{
if (reader != null)
{
reader.Close();
}
if (fs_dir != null)
{
fs_dir.Close();
}
}
return list;
}
//读取文件
public static String readFileToString(String path)
{
String str = "";
FileStream fs_dir=null;
StreamReader reader = null;
try
{
fs_dir = new FileStream(AppDomain.CurrentDomain.BaseDirectory + "/" + path, FileMode.Open, FileAccess.Read);
reader = new StreamReader(fs_dir);
str = reader.ReadToEnd();
}
catch (Exception e)
{
Tools.SysLog("readFileToString发生异常"+e.Message);
}finally
{
if (reader != null)
{
reader.Close();
}
if (fs_dir != null)
{
fs_dir.Close();
}
}
return str;
}
//读取文件
public static Byte[] readFileToByte(String path,Encoding encode)
{
Byte[] buffer = null;
FileStream fs_dir=null;
StreamReader reader = null;
try
{
fs_dir = new FileStream(path, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs_dir, encode);
int len = (int)fs_dir.Length;
buffer = new byte[len];
int size = br.Read(buffer, 0, len);
reader.Read();
}
catch (Exception e)
{
Tools.SysLog("readFileToByte-error:读取文件内容发生错误!"+e.Message);
}finally
{
if (reader != null)
{
reader.Close();
}
if (fs_dir != null)
{
fs_dir.Close();
}
}
return buffer;
}
public static object c = "";
public static String error = "";
public static void AppendLogToFile(String path,String log)
{
//锁住,防止多线程引发错误
lock (c)
{
List<String> list = new List<String>();
FileStream fs_dir = null;
StreamWriter sw = null;
try
{
fs_dir = new FileStream(AppDomain.CurrentDomain.BaseDirectory + "/" + path, FileMode.Append, FileAccess.Write);
sw = new StreamWriter(fs_dir);
sw.WriteLine(log);
}
catch (Exception e)
{
error = "文件操作发生异常!" + e.Message;
}
finally
{
if (sw != null)
{
sw.Close();
}
if (fs_dir != null)
{
fs_dir.Close();
}
}
}
}
/// <summary>
/// 注意vals为空或null会清空代理
/// </summary>
/// <param name="path"></param>
/// <param name="vals"></param>
public static void SaveProxyList(String path, Dictionary<String,Proxy>.ValueCollection vals)
{
FileStream fs_dir = null;
StreamWriter sw = null;
try
{
if (vals != null && vals.Count > 0)
{
fs_dir = new FileStream(path, FileMode.Create, FileAccess.Write);
sw = new StreamWriter(fs_dir, Encoding.UTF8);
String head = "域名或IP,代理端口,代理类型,代理账号,代理密码,是否可用,用时(毫秒),验证时间(毫秒)";
sw.WriteLine(head);
foreach (Proxy proxy in vals)
{
String line = proxy.host + "," + proxy.port + "," + proxy.proxyType + "," + proxy.username + "," + proxy.password + "," + proxy.isOk + "," + proxy.useTime + "," + proxy.checkTime;
sw.WriteLine(line);
}
}
else {
//如果为空,则删除代理
File.Delete(path);
}
}
catch (Exception e)
{
Tools.SysLog("保存代理池发生异常!" + e.Message);
}
finally
{
if (sw != null)
{
sw.Close();
}
if (fs_dir != null)
{
fs_dir.Close();
}
}
}
public static Dictionary<String,Proxy> ReadProxyList(String path)
{
Dictionary<String, Proxy> list = new Dictionary<String, Proxy>();
FileStream fs_dir = null;
StreamReader reader = null;
try
{
fs_dir = new FileStream(path, FileMode.Open, FileAccess.Read);
reader = new StreamReader(fs_dir);
String lineStr;
int line = 0;
while ((lineStr = reader.ReadLine()) != null)
{
line++;
if (line == 1) {
continue;
}
if (!lineStr.Equals(""))
{
String[] strs = lineStr.Split(',');
if (strs.Length == 2)
{
Proxy proxy = new Proxy();
proxy.host = strs[0];
proxy.port = Tools.convertToInt(strs[1]);
list.Add(proxy.host + proxy.port, proxy);
}
else if (strs.Length == 3)
{
Proxy proxy = new Proxy();
proxy.host = strs[0];
proxy.port = Tools.convertToInt(strs[1]);
proxy.proxyType = strs[2];
list.Add(proxy.host + proxy.port, proxy);
}
else if (strs.Length == 5)
{
Proxy proxy = new Proxy();
proxy.host = strs[0];
proxy.port = Tools.convertToInt(strs[1]);
proxy.proxyType = strs[2];
proxy.username = strs[3];
proxy.password = strs[4];
list.Add(proxy.host + proxy.port, proxy);
}
else if (strs.Length == 8)
{
Proxy proxy = new Proxy();
proxy.host = strs[0];
proxy.port = Tools.convertToInt(strs[1]);
proxy.proxyType = strs[2];
proxy.username = strs[3];
proxy.password = strs[4];
proxy.isOk = strs[5];
proxy.useTime = Tools.convertToInt(strs[6]);
proxy.checkTime = strs[7];
list.Add(proxy.host + proxy.port, proxy);
}
}
}
}
catch (Exception e)
{
Tools.SysLog("ReadProxyList异常"+e.Message);
}
finally
{
if (reader != null)
{
reader.Close();
}
if (fs_dir != null)
{
fs_dir.Close();
}
}
return list;
}
}
}