无法将 string[] 中的 FileInfo.Name 项添加到 List<string> 字段
Unable to add FileInfo.Name items from string[] to a List<string> field
我有以下方法,意思是将文件名添加到List<string>
,以便文件的所需顺序存储在Program
class中:
private static void ConvertPdfs()
{
// Get the word files FileInfo
FileInfo[] wordFileInfo = DataGrabber.GetHrWordFileInfo(sourceFolder, spSite, policyListName, sortBy);
foreach (FileInfo file in wordFileInfo)
{
// Create the ordered list - this adds each document to the list in the correct oder (sort in CAML query)
orderedListOfFileNames.Add(file.Name);
}
Converter convert = new Converter();
convert.ToPdf(wordFileInfo, targetPdf);
}
其中 ordereListOfFileNames
是同一 class 中的一个字段:
private static List<string> orderedListOfFileNames; // Stil static ...
当方法围绕 wordFileInfo 循环时,我看到了这个异常:
An unhandled exception of type 'System.NullReferenceException' Occurred in PdfConverter.exe
Additional information: Object reference not set to an instance of an object.
但是,我可以看到 wordFileInfo
包含 22 个项目,所有项目都有一个名称。
这里的问题是 orderedListOfFileNames
没有正确初始化吗?
您需要实例化 orderedListOfFileNames
,然后使用新的运算符向列表添加值,如下所示:
private static List<string> orderedListOfFileNames= new List<string>();
执行以下操作时不需要实例化,所以这不是问题所在。
FileInfo[] wordFileInfo = DataGrabber.GetHrWordFileInfo(sourceFolder, spSite, policyListName, sortBy);
但是当你执行上面的代码时,wordFileInfo 仍然变为空,所以 DataGrabber.GetHrWordFileInfo(sourceFolder, spSite, policyListName, sortB)
这里发生了一些事情。
我有以下方法,意思是将文件名添加到List<string>
,以便文件的所需顺序存储在Program
class中:
private static void ConvertPdfs()
{
// Get the word files FileInfo
FileInfo[] wordFileInfo = DataGrabber.GetHrWordFileInfo(sourceFolder, spSite, policyListName, sortBy);
foreach (FileInfo file in wordFileInfo)
{
// Create the ordered list - this adds each document to the list in the correct oder (sort in CAML query)
orderedListOfFileNames.Add(file.Name);
}
Converter convert = new Converter();
convert.ToPdf(wordFileInfo, targetPdf);
}
其中 ordereListOfFileNames
是同一 class 中的一个字段:
private static List<string> orderedListOfFileNames; // Stil static ...
当方法围绕 wordFileInfo 循环时,我看到了这个异常:
An unhandled exception of type 'System.NullReferenceException' Occurred in PdfConverter.exe
Additional information: Object reference not set to an instance of an object.
但是,我可以看到 wordFileInfo
包含 22 个项目,所有项目都有一个名称。
这里的问题是 orderedListOfFileNames
没有正确初始化吗?
您需要实例化 orderedListOfFileNames
,然后使用新的运算符向列表添加值,如下所示:
private static List<string> orderedListOfFileNames= new List<string>();
执行以下操作时不需要实例化,所以这不是问题所在。
FileInfo[] wordFileInfo = DataGrabber.GetHrWordFileInfo(sourceFolder, spSite, policyListName, sortBy);
但是当你执行上面的代码时,wordFileInfo 仍然变为空,所以 DataGrabber.GetHrWordFileInfo(sourceFolder, spSite, policyListName, sortB)
这里发生了一些事情。