Note that there are some explanatory texts on larger screens.

plurals
  1. POWPF StreamWriter error
    primarykey
    data
    text
    <p>When I want to write a file I get this error:</p> <p>System.ArgumentNullException: Value can't be null. Parameter name: path in System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encoding, Int32 bufferSize, Boolean checkHost) in System.IO.StreamWriter..ctor(String path) in Music_Player.Library.SongList_Save(String fileName) in "MY PATH"</p> <p>This is the code of file writer:</p> <pre><code>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) &amp;&amp; File.Exists(fileName)) { using (StreamWriter comboboxsw = new StreamWriter(fileName)) { for (int cfgitem = 0; cfgitem &lt; LB_SongList.Items.Count; cfgitem++) { comboboxsw.WriteLine(GetPath((ListBoxItem)(LB_SongList.ItemContainerGenerator.ContainerFromIndex(cfgitem)))); } comboboxsw.Close(); } } } catch (Exception e) { MessageBox.Show(e.ToString()); } } </code></pre> <p>And if this isn't enough it does write a file, but the text is written twice. //Edit it writes twice but it could be caused by other code so nevermind.</p> <p>New exception: System.ArgumentNullException: Value can't be null. Parameter name: element</p> <p>in MS.Interal.Media.VisualTreeUtils.AsNonNullVisual(DependencyObject element, Visual&amp; visual, Visual3D&amp; visual3D)</p> <p>in System.Windows.Media.VisualTreeHelper.GetChildernCount(DependencyObject reference)</p> <p>in Music_Player.Library.FindVisualChild[childItem](DependencyObject obj) in "MY PATH"</p> <p>in Music_Player.Library.GetPath(ListBoxItem lb) in "MY PATH"</p> <p>in Music_Player.Library.SongList_Save(String fileName) in "MY PATH"</p> <p>All three methods below:</p> <pre><code>private childItem FindVisualChild&lt;childItem&gt;(DependencyObject obj) where childItem : DependencyObject { for (int i = 0; i &lt; VisualTreeHelper.GetChildrenCount(obj); i++) { DependencyObject child = VisualTreeHelper.GetChild(obj, i); if (child != null &amp;&amp; child is childItem) return (childItem)child; else { childItem childOfChild = FindVisualChild&lt;childItem&gt;(child); if (childOfChild != null) return childOfChild; } } return null; } private string GetPath(ListBoxItem lb) { ListBoxItem myListBoxItem = (ListBoxItem)(lb); ContentPresenter myContentPresenter = FindVisualChild&lt;ContentPresenter&gt;(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 &lt; LB_SongList.Items.Count; cfgitem++) { comboboxsw.WriteLine(GetPath((ListBoxItem)(LB_SongList.ItemContainerGenerator.ContainerFromIndex(cfgitem)))); } comboboxsw.Close(); } } } catch (Exception e) { MessageBox.Show(e.ToString()); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload