我对OpenFileDialog (在Windows中)的使用没有问题。我无法确定在Silverlight( WPF)中使用OpenFileDialog的错误在哪里。在我的代码中,我对这个字符串感兴趣,在需要显示路径的地方:
var lines = File.ReadLines(fileStream);Silverlight的所有代码(不工作):
private void Button_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog opendialog = new OpenFileDialog();
System.IO.Stream fileStream = opendialog.File.OpenRead();
if (opendialog.ShowDialog() == true)
{
var lines = File.ReadLines(fileStream);
string pattern = @"set vrouter ""([\w-]+)""";
var matches =
lines.SelectMany(line => Regex.Matches(line, pattern)
.Cast<Match>()).Where(m => m.Success)
.Select(m => m.Groups[1].Value)
.Distinct();
foreach (String match in matches)
{
textBox1.AppendText(match + Environment.NewLine);
}
}
}
}}
Windows的代码(工作良好):
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog opendialog = new OpenFileDialog();
if (opendialog.ShowDialog() == DialogResult.OK)
{
var lines = File.ReadLines(opendialog.FileName);
string pattern = @"set vrouter ""([\w-]+)""";
var matches =
lines.SelectMany(line=> Regex.Matches(line, pattern)
.Cast<Match>()).Where(m => m.Success)
.Select(m => m.Groups[1].Value)
.Distinct();
foreach (String match in matches)
{
textBox1.AppendText(match + Environment.NewLine);
}
}
}发布于 2014-02-11 21:33:20
Silverlight默认使用提升的权限运行(简单地在沙箱中这样说),这意味着
var lines = File.ReadLines(fileStream);不能工作,有两个原因:
基于上述,您的问题可以通过以下代码解决:
OpenFileDialog opendialog = new OpenFileDialog();
if (opendialog.ShowDialog() == true)
{
string text = string.Empty;
using (StreamReader reader = opendialog.File.OpenText())
{
text = reader.ReadToEnd();
}
// do stuff here
}或msdn提供的另一个选项:http://msdn.microsoft.com/en-us/library/cc221415(v=vs.95).aspx
发布于 2014-02-13 13:45:55
发现我在寻找!这个示例(使用OpenFileDialog Silverlight)运行得很好:
private void Button_Click(object sender, EventArgs e)
{
OpenFileDialog opendialog = new OpenFileDialog();
opendialog.Multiselect = true;
bool? dialogResult = opendialog.ShowDialog();
if (dialogResult.HasValue && dialogResult.Value)
{
textBox1.Text = string.Empty;
foreach (var file in opendialog.Files)
{
Stream fileStream = file.OpenRead();
using (StreamReader reader = new StreamReader(fileStream))
{
string pattern = @"set vrouter ""([\w-]+)""";
while (!reader.EndOfStream)
{
var matches =
Regex.Matches(reader.ReadToEnd(), pattern)
.Cast<Match>().Where(m => m.Success)
.Select(m => m.Groups[1].Value)
.Distinct();
foreach (var match in matches)
{
textBox1.Text += val;
}https://stackoverflow.com/questions/21712817
复制相似问题