在我的“文件资源管理器”应用程序中,我尝试在任何文件夹中只显示xml文件--每次打开文件夹时,我只想显示present.So文件。我查看过这个站点和互联网,有很多示例,但在C#中没有找到。我还尝试修改其他示例,以满足我的需要,但我无法实现IFileNameFilter class.Does --有人知道怎么做吗?我试着实现它,但不断得到构建错误。
在下面的代码中,我有一个ListFragment类,它显示文件:
public override void OnListItemClick(ListView l, View v, int position, long id)
{
try{
var fileSystemInfo = _adapter.GetItem(position);
if (fileSystemInfo.IsFile())
{
// Do something with the file. In this case we just pop some toast.
Log.Verbose("FileListFragment", "The file {0} was clicked.", fileSystemInfo.FullName);
Toast.MakeText(Activity, "You selected file " + fileSystemInfo.FullName, ToastLength.Short).Show();
OnFileClick (fileSystemInfo);
}
else
{
// Dig into this directory, and display it's contents
RefreshFilesList(fileSystemInfo.FullName);
}
}catch(Exception e){
Log.Error (TAG,e.ToString());
}
base.OnListItemClick(l, v, position, id);
}
public void RefreshFilesList(string directory)
{
//GenericExtFilter filter = new GenericExtFilter(extension);
IList<FileSystemInfo> visibleThings = new List<FileSystemInfo>();
var dir = new DirectoryInfo(directory);
try
{
foreach (var item in dir.GetFileSystemInfos().Where(item => item.IsVisible()))
{
visibleThings.Add(item);
}
}
catch (Exception ex)
{
Log.Error("FileListFragment", "Couldn't access the directory " + _directory.FullName + "; " + ex);
Toast.MakeText(Activity, "Problem retrieving contents of " + directory, ToastLength.Long).Show();
return;
}
_directory = dir;
_adapter.AddDirectoryContents(visibleThings);
// If we don't do this, then the ListView will not update itself when the data set
// in the adapter changes. It will appear to the user that nothing has happened.
ListView.RefreshDrawableState();
Log.Verbose("FileListFragment", "Displaying the contents of directory {0}.", directory);
}发布于 2015-04-15 11:06:57
我用一个简单得多的解决方案解决了这个问题,我没有在上面的IFileNameFilter ( all.Inside,RefreshFilesList方法)之后使用它,我做了以下修改:
foreach (var item in dir.GetFileSystemInfos().Where(item => item.IsVisible()))
{
if(item.IsDirectory())
{
visibleThings.Add(item);
}else if(item.IsFile())
{
bool isXmlFile = item.Extension.ToLower().EndsWith("xml");
if(isXmlFile)
{
visibleThings.Add(item);
}
}
}这现在显示了所有文件夹和存在的任何xml文件。
发布于 2015-04-11 04:16:52
如果我正确理解,您希望创建将过滤IFilenameFilter文件的XML。
public class XmlFileFilter : Java.Lang.Object, IFilenameFilter
{
public bool Accept(File dir, string filename)
{
return filename.ToLower().EndsWith(".xml");
}
}https://stackoverflow.com/questions/29563592
复制相似问题