簡體   English   中英

WPF StreamWriter錯誤

[英]WPF StreamWriter error

當我要寫入文件時,出現此錯誤:

System.ArgumentNullException:值不能為null。 參數名稱:System.IO.StreamWriter..ctor中的路徑(字符串路徑,布爾值附加,編碼編碼,System.IO.StreamWriter..ctor中的Int32 bufferSize,布爾值CheckHost),在Music_Player.Library.SongList_Save(String)中文件名)在“我的路徑”中

這是文件編寫器的代碼:

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());
        }
    }

如果這還不夠,它會寫入文件,但是文本會被寫入兩次。 //編輯它會寫兩次,但是可能是由其他代碼引起的,所以不要緊。

新的異常:System.ArgumentNullException:值不能為null。 參數名稱:元素

在MS.Interal.Media.VisualTreeUtils.AsNonNullVisual(DependencyObject元素,Visual&visual,Visual3D&visual3D)

在System.Windows.Media.VisualTreeHelper.GetChildernCount(DependencyObject參考)

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

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

在“我的路徑”中的Music_Player.Library.SongList_Save(String fileName)中

以下所有三種方法:

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());
        }
    }

聽起來像path為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());
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM