尝试浏览我的文件和我的驱动器文件夹时出错
Error when trying to explore my files and Folder of my Drive
我正在尝试浏览我的文件和我的驱动器的文件夹但是我得到 error.I 已经写了一个方法来获取我的驱动器详细信息,然后是一个方法来获取我的驱动器的详细信息(即文件和文件夹)
- DirectoryList 的集合 属性 处出错:异常
抛出了“System.WhosebugException”类型的。
这是我的代码:
public class FilePath
{
public string FileName { get; set; }
public string inputFilePath { get; set; }
}
public class OpenDataViewModel : BindableBase
{
public OpenDataViewModel()
{
ListDrives();
}
private FilePath _selecteddirectory;
public FilePath SelectedDirectory
{
get { return _selecteddirectory; }
set
{
SetProperty(ref _selecteddirectory, value);
ListDirectoryContent(_selecteddirectory);
}
}
public List<FilePath> _directoryList;
public List<FilePath> DirectoryList
{
get { return _directoryList; }
set { SetProperty(ref _directoryList, value); }
}
private List<FilePath> _fileList;
public List<FilePath> FileList
{
get { return _fileList; }
set { SetProperty(ref _fileList, value); }
}
public void ListDrives()
{
DirectoryList = new List<FilePath>();
foreach (var drive in Directory.GetLogicalDrives())
{
DirectoryList.Add(new FilePath()
{
FileName = drive.Remove(1, drive.LastIndexOf('\') ) ,
inputFilePath = Convert.ToString(drive)
});
}
}
public void ListDirectoryContent(FilePath inputPath)
{
DirectoryList = new List<FilePath>();
foreach (var directory in Directory.GetDirectories(inputPath.inputFilePath))
{
DirectoryList.Add(new FilePath()
{
FileName = directory.Remove(0,3),
inputFilePath = Convert.ToString(directory)
});
}
FileList = new List<FilePath>();
foreach (var fileData in Directory.GetFiles(inputPath.inputFilePath))
{
FileList.Add(new FilePath()
{
FileName = fileData.Remove(0, 3),
inputFilePath = Convert.ToString(fileData)
}) ;
}
}
这是我的 Xmal:
<ListView ItemsSource="{Binding DirectoryList}" Grid.Row="2" Grid.Column="0" Background="Aqua" SelectedValue="{Binding SelectedDirectory}" >
<ListView.View>
<GridView>
<GridViewColumn DisplayMemberBinding="{Binding FileName}" Header="Folder" Width="250" ></GridViewColumn>
</GridView>
</ListView.View>
</ListView>
当设置 SelectedDirectory
时,您会更新 DirectoryList
,这反过来会导致 ListView
更新 SelectedDirectory
,因此您的 WhosebugException
。
你至少要改变的事情:
- 仅当所选目录实际更改时才更新
DirectoryList
- 使指向同一文件的
FilePath
个实例相等
广告 1:
public FilePath SelectedDirectory
{
get => _selecteddirectory;
set
{
if (SetProperty(ref _selecteddirectory, value))
ListDirectoryContent(_selecteddirectory);
}
}
广告 2:
实施 operator==
和 Equals
并使其成为 readonly struct
而不是 class
.
我正在尝试浏览我的文件和我的驱动器的文件夹但是我得到 error.I 已经写了一个方法来获取我的驱动器详细信息,然后是一个方法来获取我的驱动器的详细信息(即文件和文件夹)
- DirectoryList 的集合 属性 处出错:异常 抛出了“System.WhosebugException”类型的。
这是我的代码:
public class FilePath
{
public string FileName { get; set; }
public string inputFilePath { get; set; }
}
public class OpenDataViewModel : BindableBase {
public OpenDataViewModel()
{
ListDrives();
}
private FilePath _selecteddirectory;
public FilePath SelectedDirectory
{
get { return _selecteddirectory; }
set
{
SetProperty(ref _selecteddirectory, value);
ListDirectoryContent(_selecteddirectory);
}
}
public List<FilePath> _directoryList;
public List<FilePath> DirectoryList
{
get { return _directoryList; }
set { SetProperty(ref _directoryList, value); }
}
private List<FilePath> _fileList;
public List<FilePath> FileList
{
get { return _fileList; }
set { SetProperty(ref _fileList, value); }
}
public void ListDrives()
{
DirectoryList = new List<FilePath>();
foreach (var drive in Directory.GetLogicalDrives())
{
DirectoryList.Add(new FilePath()
{
FileName = drive.Remove(1, drive.LastIndexOf('\') ) ,
inputFilePath = Convert.ToString(drive)
});
}
}
public void ListDirectoryContent(FilePath inputPath)
{
DirectoryList = new List<FilePath>();
foreach (var directory in Directory.GetDirectories(inputPath.inputFilePath))
{
DirectoryList.Add(new FilePath()
{
FileName = directory.Remove(0,3),
inputFilePath = Convert.ToString(directory)
});
}
FileList = new List<FilePath>();
foreach (var fileData in Directory.GetFiles(inputPath.inputFilePath))
{
FileList.Add(new FilePath()
{
FileName = fileData.Remove(0, 3),
inputFilePath = Convert.ToString(fileData)
}) ;
}
}
这是我的 Xmal:
<ListView ItemsSource="{Binding DirectoryList}" Grid.Row="2" Grid.Column="0" Background="Aqua" SelectedValue="{Binding SelectedDirectory}" >
<ListView.View>
<GridView>
<GridViewColumn DisplayMemberBinding="{Binding FileName}" Header="Folder" Width="250" ></GridViewColumn>
</GridView>
</ListView.View>
</ListView>
当设置 SelectedDirectory
时,您会更新 DirectoryList
,这反过来会导致 ListView
更新 SelectedDirectory
,因此您的 WhosebugException
。
你至少要改变的事情:
- 仅当所选目录实际更改时才更新
DirectoryList
- 使指向同一文件的
FilePath
个实例相等
广告 1:
public FilePath SelectedDirectory
{
get => _selecteddirectory;
set
{
if (SetProperty(ref _selecteddirectory, value))
ListDirectoryContent(_selecteddirectory);
}
}
广告 2:
实施 operator==
和 Equals
并使其成为 readonly struct
而不是 class
.