这是方法:
public void CheckFileType(string directoryPath)
{
var files = Directory.GetFiles(directoryPath).GetEnumerator();
while (files.MoveNext())
{
//get file extension
string fileExtension = Path.GetExtension(Convert.ToString(files.Current));
//get file name without extenstion
string fileName =
Convert.ToString(files.Current).Replace(fileExtension, string.Empty);
//Check for JPG File Format
if (fileExtension == ".jpg" || fileExtension == ".JPG")
// or // ImageFormat.Jpeg.ToString()
{
try
{
//OCR Operations ...
MODI.Document md = new MODI.Document();
md.Create(Convert.ToString(files.Current));
md.OCR(MODI.MiLANGUAGES.miLANG_ENGLISH, true, true);
MODI.Image image = (MODI.Image)md.Images[0];
//create text file with the same Image file name
FileStream createFile =
new FileStream(fileName + ".txt", FileMode.CreateNew);
//save the image text in the text file
StreamWriter writeFile = new StreamWriter(createFile);
writeFile.Write(image.Layout.Text);
writeFile.Close();
}
catch (Exception exc)
{
//uncomment the below code to see the expected errors
w.Write(exc.ToString() + Environment.NewLine);
}
}
}
w.Close();
}
它在变量 files.Current 包含文件名时工作.
然后我得到了异常:
string fileName = Convert.ToString(files.Current).Replace(fileExtension, string.Empty);
字符串长度不能为零
System.ArgumentException 未处理 HResult=-2147024809
消息=字符串长度不能为零。参数名称:oldValue
Source=mscorlib ParamName=oldValue