首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WPF StreamWriter错误

WPF StreamWriter错误
EN

Stack Overflow用户
提问于 2013-11-17 01:55:23
回答 1查看 1K关注 0票数 0

当我想要写一个文件时,我得到这个错误:

System.ArgumentNullException:值不能为null。参数名称:MY path中System.IO.StreamWriter..ctor(String path)中System.IO.StreamWriter..ctor(String path,Boolean append,Encoding encoding,Int32 bufferSize,Boolean checkHost)中PATH(String PATH)中Music_Player.Library.SongList_Save(String fileName)中的PATH

这是文件写入器的代码:

代码语言:javascript
复制
private void AddSong(string path)
    {
        DataContext = new Song();
        Song _songAdd = new Song();
        FileInfo _song = new FileInfo(path);


        _songAdd.SongLengh = MainWindow._MusicPlayer.TransformToTime(MainWindow._MusicPlayer.GetSongMaxLength(path));
        _songAdd.SongName = System.IO.Path.GetFileNameWithoutExtension(_song.Name);
        _songAdd.SongPath = path;
        LB_SongList.Items.Add(_songAdd);
        SongList_Save(SelectedPlaylist);
    }
private void LB_SongList_Drop(object sender, DragEventArgs e)
    {
        String[] file = e.Data.GetData(DataFormats.FileDrop, true) as String[];
        foreach (var path in file)
        {
            if (MainWindow._MusicPlayer.GetSongMaxLength(path) != -1)
            {
                AddSong(path);
            }
        }
    }

public void SongList_Save(String fileName)
    {
        try
        {
            if (!String.IsNullOrEmpty(fileName) && File.Exists(fileName))
            {
                using (StreamWriter comboboxsw = new StreamWriter(fileName))
                {
                    for (int cfgitem = 0; cfgitem < LB_SongList.Items.Count; cfgitem++)
                    {
                        comboboxsw.WriteLine(GetPath((ListBoxItem)(LB_SongList.ItemContainerGenerator.ContainerFromIndex(cfgitem))));
                    }
                    comboboxsw.Close();
                }
            }
        }
        catch (Exception e)
        {
            MessageBox.Show(e.ToString());
        }
    }

如果这还不够,它会写一个文件,但文本会写两次。//Edit它写了两次,但它可能是由其他代码引起的,所以没关系。

新异常: System.ArgumentNullException:值不能为空。参数名称:元素

在MS.Interal.Media.VisualTreeUtils.AsNonNullVisual(DependencyObject元素、视觉和视觉、Visual3D和visual3D中)

在System.Windows.Media.VisualTreeHelper.GetChildernCount(DependencyObject参考中)

在"MY PATH“中的Music_Player.Library.FindVisualChildchildItem

在"MY PATH“中的Music_Player.Library.GetPath(ListBoxItem lb)中

在"MY PATH“中的Music_Player.Library.SongList_Save(String fileName)中

下面的所有三种方法:

代码语言:javascript
复制
private childItem FindVisualChild<childItem>(DependencyObject obj) where childItem : DependencyObject
    {
        for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
        {
            DependencyObject child = VisualTreeHelper.GetChild(obj, i);
            if (child != null && child is childItem)
                return (childItem)child;
            else
            {
                childItem childOfChild = FindVisualChild<childItem>(child);
                if (childOfChild != null)
                    return childOfChild;
            }
        }
        return null;
    }
private string GetPath(ListBoxItem lb)
    {
        ListBoxItem myListBoxItem = (ListBoxItem)(lb);
        ContentPresenter myContentPresenter = FindVisualChild<ContentPresenter>(myListBoxItem);
        DataTemplate myDataTemplate = myContentPresenter.ContentTemplate;
        Label target = (Label)myDataTemplate.FindName("SongPath", myContentPresenter);
        return (string)target.Content;
    }
public void SongList_Save(String fileName)
    {
        try
        {
            if (!String.IsNullOrEmpty(fileName))
            {
                using (StreamWriter comboboxsw = new StreamWriter(fileName))
                {
                    for (int cfgitem = 0; cfgitem < LB_SongList.Items.Count; cfgitem++)
                    {
                        comboboxsw.WriteLine(GetPath((ListBoxItem)(LB_SongList.ItemContainerGenerator.ContainerFromIndex(cfgitem))));
                    }
                    comboboxsw.Close();
                }
            }
        }
        catch (Exception e)
        {
            MessageBox.Show(e.ToString());
        }
    }
EN

回答 1

Stack Overflow用户

发布于 2013-11-17 02:05:41

听起来path是null。在使用null之前,请尝试检查它。

代码语言:javascript
复制
public void SongList_Save(String fileName)
{
    try
    {
        if (!String.IsNullOrEmpty(fileName))
        {
            using (StreamWriter comboboxsw = new StreamWriter(fileName))
            {
                for (int cfgitem = 0; cfgitem < LB_SongList.Items.Count; cfgitem++)
                {
                    comboboxsw.WriteLine(GetPath((ListBoxItem)(LB_SongList.ItemContainerGenerator.ContainerFromIndex(cfgitem))));
                }
                comboboxsw.Close();
            }
        }
    }
    catch (Exception e)
    {
        MessageBox.Show(e.ToString());
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20021770

复制
相关文章

相似问题

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