火车浏览器官方论坛
标题:
C#代码片段集合-判断文件是否存在并删除&&C#代码查看本地图片大小KB,长宽比
[打印本页]
作者:
cgq9845
时间:
2019-12-22 11:06
标题:
C#代码片段集合-判断文件是否存在并删除&&C#代码查看本地图片大小KB,长宽比
本帖最后由 cgq9845 于 2019-12-22 16:45 编辑
//判断文件是否存在浏览器参数@stringDic["p_file"];格式D:\火车浏览器7.2\tup\1.jpg,,返回值stringDic["wenjianisexist"] 布尔值
//
public static void Execute(Dictionary<string, string> stringDic,Dictionary<string, List<string>> listDic, Dictionary<string, int> intDic)
{
string filelocation = @stringDic["p_file"];
//检测是否存在
bool exist= File.Exists(filelocation);
stringDic["wenjianisexist"] = exist.ToString();
// 1、首先判断文件或者文件路径是否存在
if (File.Exists(filelocation))
{
// 2、根据路径字符串判断是文件还是文件夹
FileAttributes attr = File.GetAttributes(filelocation);
// 3、根据具体类型进行删除
if (attr == FileAttributes.Directory)
{
// 3.1、删除文件夹
Directory.Delete(filelocation, true);
}
else
{
// 3.2、删除文件
File.Delete(filelocation);
}
File.Delete(filelocation);
}
}
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//判断文件是否存在浏览器参数@stringDic["p_file"];格式D:\火车浏览器7.2\tup\1.jpg,,返回值stringDic["wenjianisexist"] 布尔值
public static void Execute(Dictionary<string, string> stringDic,Dictionary<string, List<string>> listDic, Dictionary<string, int> intDic)
{
string filelocation = @stringDic["p_file"];
//检测是否存在
bool exist= File.Exists(filelocation);
stringDic["wenjianisexist"] = exist.ToString();
}
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//C#代码查看本地图片大小KB,长宽比
// @stringDic["V_PATH"];====D:\\xxxxx\\tup\\1.jpg------string file = @stringDic["V_PATH"] + @"pic\" + stringDic["p_file"];
public static void Execute(Dictionary<string, string> stringDic,Dictionary<string, List<string>> listDic, Dictionary<string, int> intDic)
{
string file = @stringDic["V_PATH"];
System.Drawing.Image localimage = System.Drawing.Image.FromFile(@file);
stringDic["p_Width"] = localimage.Width.ToString();
stringDic["p_Height"] = localimage.Height.ToString();
FileInfo fileInfo = new FileInfo(@file);
double length = Convert.ToDouble(fileInfo.Length);
int Size = (int)length / 1024 ;
stringDic["p_Size"] = Size.ToString();
//MessageBox.Show(string.Format("File:{0},\n\rwidth:{1},\n\rheight:{2},\n\rSize:{3}",file, localimage.Width,localimage.Height,Size));
}
[attach]702[/attach]
["V_PATH"] 这些是浏览器的变量。 格式为stringDic["*******"];
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
///获取指定文件夹下的所有文件的文件名String path = @"D:\xxxx\tup";注意路径不能含有中文,或者放到浏览器变量里。string path = @stringDic["p_file"];
//浏览器变量p_file=D:\xxxx\tup 注意为目录注意路径不能含有中文,而不是文件。
//导出的文件放在软件运行目录,我的是D:\火车浏览器7.2\RunTime\fileList.txt
public static void Execute(Dictionary<string, string> stringDic,Dictionary<string, List<string>> listDic, Dictionary<string, int> intDic)
{
String path = @"D:\xxxx\tup";
DirectoryInfo folder = new DirectoryInfo(path);
StreamWriter sw = null;
sw = new StreamWriter(new FileStream("fileList.txt", FileMode.Append));
foreach (FileInfo file in folder.GetFiles("*.*"))
{
//sw.WriteLine("文件名:" + file.FullName);
sw.WriteLine(file.FullName);
}
}
[attach]703[/attach]
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
C#代码还是很强大的,大家也都多多发帖啊,互通有无。QQ984511989使用交流。
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
欢迎光临 火车浏览器官方论坛 (http://bbs.locoyposter.com/)
Powered by Discuz! X3.2