System.ArgumentException未处理 - String的长度不能为零

时间:2013-09-05 22:06:49

标签: c# asp.net webforms

这是方法:

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包含文件名时它工作:RadarGifAnimatoion,文件类型只是:文件 我在文件上做了属性,在Type i下只看到:File 这个文件大小是32个字节,我猜文件是坏的还是空的。

然后即时获取异常:

string fileName = Convert.ToString(files.Current).Replace(fileExtension, string.Empty);
  

字符串不能为零长度

     

System.ArgumentException未处理HResult = -2147024809
  Message = String的长度不能为零。参数名称:oldValue
  Source = mscorlib ParamName = oldValue

1 个答案:

答案 0 :(得分:3)

Replace中的

oldValue(第一个参数)不能为零长度 如果是抛出ArgumentException

Microsofts documentation for Replace包含有关函数可以抛出的异常的信息。