我正在将我的应用程序部署到 Windows 7、Windows 10 和 Linux。有一次,我使用 OpenFileDialog 允许用户选择文件路径。这在 Windows 10 和 Linux 中有效,但在 Windows 7 中运行时,会引发 ArgumentException。
我尝试通过显示异常消息来查看异常,即“值不在预期范围内”。我不确定这意味着什么。它在 Windows 10 中运行良好,所以我不知道为什么它在这里不能正常运行。
我有一个 GetPath() 方法,它使用 OpenFileDialog 获取选定的路径,还有一个按钮单击事件,它调用 GetPath() 并将结果设置为局部变量。
public async Task<string> GetPath()
{
OpenFileDialog dialog = new OpenFileDialog();
dialog.Filters.Add(new FileDialogFilter() { Name = "Csv", Extensions = { "csv" } });
string[] result = await dialog.ShowAsync(this); //sets opened file to a stream
string stream = string.Join(" ", result); //converts string[] to string
return stream; //returns path to selected file
}
private async void MasterBrowse_Clicked(object sender, RoutedEventArgs args)
{
string getPath = string.Empty;
try
{
getPath = await GetPath();
}
catch (ArgumentException e)
{
await MessageBox.Show(this, "Make sure to select a file before continuing\n" + "Exception: " + e.Message, "Error: incorrect file", MessageBox.MessageBoxButtons.Ok);
}
}
预期:GetPath() 应该 OpenFileDialog 并将所选路径保存为字符串,然后返回它。MasterBrowse_Clicked() 应该得到返回的字符串。
实际:调用 OpenFileDialog.ShowAsync() 时引发 ArgumentException。