当我想要写一个文件时,我得到这个错误:
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
这是文件写入器的代码:
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)中
下面的所有三种方法:
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());
}
}发布于 2013-11-17 02:05:41
听起来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());
}
}https://stackoverflow.com/questions/20021770
复制相似问题