You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

101 lines
2.7 KiB
C#

using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace InterfaceWorkAreaData
{
public class CheckFilePath
{
public static bool IsValidFilePath(string path)
{
if (string.IsNullOrWhiteSpace(path))
return false;
try
{
// 检查路径语法是否合法
string fullPath = Path.GetFullPath(path);
// 检查是否包含非法字符
if (path.IndexOfAny(Path.GetInvalidPathChars()) >= 0)
return false;
// 可选:检查文件名是否为空(如路径为 "C:\\" 是目录,不是文件)
string fileName = Path.GetFileName(fullPath);
if (string.IsNullOrEmpty(fileName) || fileName.Contains("."))
return true; // 允许无扩展名或有扩展名的文件名
else
return false; // 路径末尾无文件名(可能是目录)
}
catch (ArgumentException)
{
return false;
}
catch (PathTooLongException)
{
return false;
}
catch (NotSupportedException)
{
return false;
}
}
public static bool IsValidAndFileExists(string path)
{
return IsValidFilePath(path) && File.Exists(path);
}
}
public enum eDataSourceType
{
empty,
kep
}
public sealed class workAreaDb
{
private workAreaDb()
{
}
private static readonly Lazy<workAreaDb> _instance = new Lazy<workAreaDb>(() => new workAreaDb());
public static workAreaDb Instance => _instance.Value;
public bool Connected(string dsname, eDataSourceType type)
{
bool b;
if(type == eDataSourceType.empty)
{
this.workData = new emptyWorkAreaData();
b = false;
}
else
{
string dbfilepath = dsname + "\\sys.db";// Path.GetDirectoryName(dsname + "\\sys.db");
if (CheckFilePath.IsValidAndFileExists(dbfilepath))
{
this.workData = new KepWorkAreaData();
b = this.workData.Conn(dsname);
}
else
{//检查工区的数据库路径失败
this.workData = new emptyWorkAreaData();
b = false;
}
}
return b;
}
public IWorkAreaData workData { get; private set; }
}
}