Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WPF StreamWriter错误_C#_Wpf - Fatal编程技术网

C# WPF StreamWriter错误

C# WPF StreamWriter错误,c#,wpf,C#,Wpf,当我想写一个文件时,我会遇到以下错误: System.ArgumentNullException:值不能为null。 参数名称:路径 在System.IO.StreamWriter..ctor中(字符串路径、布尔追加、编码编码、Int32 bufferSize、布尔校验主机) 在System.IO.StreamWriter..ctor中(字符串路径) 在Music\u Player.Library.SongList\u“我的路径”中保存(字符串文件名) 这是文件编写器的代码: private v

当我想写一个文件时,我会遇到以下错误:

System.ArgumentNullException:值不能为null。 参数名称:路径 在System.IO.StreamWriter..ctor中(字符串路径、布尔追加、编码编码、Int32 bufferSize、布尔校验主机) 在System.IO.StreamWriter..ctor中(字符串路径) 在Music\u Player.Library.SongList\u“我的路径”中保存(字符串文件名)

这是文件编写器的代码:

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());
        }
    }
private void AddSong(字符串路径)
{
DataContext=新歌曲();
歌曲_songAdd=新歌();
FileInfo _song=新的FileInfo(路径);
_songAdd.SongLengh=MainWindow.\u MusicLayer.TransformToTime(MainWindow.\u MusicLayer.GetSongMaxLength(路径));
_songAdd.SongName=System.IO.Path.GetFileNameWithoutExtension(_song.Name);
_songAdd.SongPath=path;
LB_SongList.Items.Add(_songAdd);
歌曲列表\保存(选择播放列表);
}
私有void LB_歌曲列表_Drop(对象发送方,DragEventArgs e)
{
String[]file=e.Data.GetData(DataFormats.FileDrop,true)作为字符串[];
foreach(文件中的var路径)
{
如果(主窗口._MusicPlayer.GetSongMaxLength(路径)!=-1)
{
AddSong(路径);
}
}
}
公共无效歌曲列表\保存(字符串文件名)
{
尝试
{
如果(!String.IsNullOrEmpty(fileName)和&File.Exists(fileName))
{
使用(StreamWriter ComboxSW=新StreamWriter(文件名))
{
对于(int cfgitem=0;cfgitem
如果这还不够,它会写一个文件,但是文本会写两次。 //编辑它会写两次,但它可能是由其他代码引起的,所以不要担心

新的例外情况: System.ArgumentNullException:值不能为null。 参数名称:元素

在MS.Interal.Media.visualtreutils.AsNonNullVisual中(DependencyObject元素、Visual3D和Visual3D)

在System.Windows.Media.VisualTreeHelper.GetChilderCount(DependencyObject引用)中

在Music_Player.Library.FindVisualChild[childItem](DependencyObject obj)中的“我的路径”

在Music_Player.Library.GetPath(ListBoxItem lb)中的“我的路径”中

在Music\u Player.Library.SongList\u“我的路径”中保存(字符串文件名)

以下三种方法:

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());
        }
    }
私有childItem FindVisualChild(DependencyObject obj),其中childItem:DependencyObject
{
for(int i=0;i
听起来路径是
null
。在使用之前,请尝试检查
null

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());
    }
}
public void歌曲列表\u保存(字符串文件名)
{
尝试
{
如果(!String.IsNullOrEmpty(文件名))
{
使用(StreamWriter ComboxSW=新StreamWriter(文件名))
{
对于(int cfgitem=0;cfgitem
Easy one,调试您的应用程序,您将看到,
fileName
为null或空,正如异常已经指出的那样…;)但我在调用时总是定义文件名。它用它应该写的名字写文件,并写了两行相同的代码,但例外情况是相反的。检查您的代码。@KubaWasilczyk请参见下面的更新答案。。但是,您是在尝试更新现有文件,还是希望每次写入时都创建一个新文件?两者都是。若文件存在,则应删除旧文件并写入新文件。如果文件不存在,代码不会写任何东西,尽管它可以工作。省略
文件。存在(…)
部分,一切都会好起来。还是一样。我将添加另一段可能导致它的代码。@DHN是的,刚刚意识到正在创建新文件而不是更新现有文件。谢谢