单击添加按钮,如图
添加文件成功,如图
代码如下
private void AdditionFliebin(object sender, RoutedEventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "txt files(*.*)|*.*";
byte[] readArray = new byte[20];
openFileDialog.Multiselect = true;
//如果用户没有选择文件并确定则直接返回
if (openFileDialog.ShowDialog() != true)
{
return;
}
string fileName = "";
//获取选中的所有文件
string[] allFilePaths = openFileDialog.FileNames;
foreach (var srcPath in allFilePaths)
{
//文件是否重复
bool isRepetition = false;
FileStream fileStreamReadbin = new FileStream(srcPath, FileMode.OpenOrCreate, FileAccess.Read);
//文件名
fileName = System.IO.Path.GetFileName(srcPath);
//分隔文件名
string[] separatefileName = fileName.Split('.');
if (separatefileName.Length > 1 && separatefileName[1].Length >= 15)
{
float length = (float)fileStreamReadbin.Length / 20;
if (length > 0)
{
if (length - (int)length == 0)
{
fileStreamReadbin.Read(readArray, 0, 20);
//关闭文件流
fileStreamReadbin.Close();
if (readArray.Last() == 64)
{
//文件路径
string filePath = System.IO.Path.GetDirectoryName(srcPath) + "\\";
foreach (var binFile in BinFormatFile)
{
if (binFile.FileName == fileName)
{
isRepetition = true;
break;
}
}
if (!isRepetition)
{
BinFormatFile.Add(new MianFileModel
{
FileName = fileName,
FilePath = filePath
});
}
else
{
MessageBox.Show("文件名重复:" + fileName);
}
}
else
{
MessageBox.Show("添加的文件格式不正确:" + fileName);
}
}
else
{
MessageBox.Show("请检查文件长度格式是否符合:" + fileName);
}
}
else
{
MessageBox.Show("请检查文件是否为空:" + fileName);
}
}
else
{
MessageBox.Show("文件名格式不对:" + fileName);
}
}
}