首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >OpenFileDialog银灯

OpenFileDialog银灯
EN

Stack Overflow用户
提问于 2014-02-11 20:56:37
回答 2查看 383关注 0票数 0

我对OpenFileDialog (在Windows中)的使用没有问题。我无法确定在Silverlight( WPF)中使用OpenFileDialog的错误在哪里。在我的代码中,我对这个字符串感兴趣,在需要显示路径的地方:

代码语言:javascript
复制
var lines = File.ReadLines(fileStream);

Silverlight的所有代码(不工作):

代码语言:javascript
复制
        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的代码(工作良好):

代码语言:javascript
复制
        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);
                }
            }



    }
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-02-11 21:33:20

Silverlight默认使用提升的权限运行(简单地在沙箱中这样说),这意味着

代码语言:javascript
复制
var lines = File.ReadLines(fileStream);

不能工作,有两个原因:

  1. 参数应该是字符串路径,而不是silverlight api中的流。
  2. File.ReadLines将不能工作,因为它只对受信任的应用程序起作用。

基于上述,您的问题可以通过以下代码解决:

代码语言:javascript
复制
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

票数 1
EN

Stack Overflow用户

发布于 2014-02-13 13:45:55

发现我在寻找!这个示例(使用OpenFileDialog Silverlight)运行得很好:

代码语言:javascript
复制
        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;
                        }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21712817

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档