我一直试图写这个以便列出我的文件夹,附加部分不起作用并重写整个事情,如果我尝试列出其他内容而不退出它给出的应用程序"文件正在使用"因为我在每个循环结束时使用了close(),所以对我没有意义的错误。
我在这里检查了几乎所有的Microsoft文档和大量帖子,但无法找到我的答案。
The Picture that shows the Error I get
提前多多感谢!
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace Anime_Lister
{
public partial class Anime0Lister : Form
{
public void Appen()
{
using (System.IO.StreamWriter file = new System.IO.StreamWriter(path0save.Text, true))
{
DirectoryInfo directory = new DirectoryInfo(path0tb.Text);
DirectoryInfo[] directory0Arr = directory.GetDirectories();
foreach (DirectoryInfo dir in directory0Arr)
{
String Parent = Convert.ToString(dir.Parent);
String Name = Convert.ToString(dir.Name);
String Root = Convert.ToString(dir.Root);
File.AppendAllText(path0save.Text, Parent);
File.AppendAllText(path0save.Text, " || ");
File.AppendAllText(path0save.Text, Name + System.Environment.NewLine);
File.AppendAllText(path0save.Text, Root + System.Environment.NewLine);
}
}
}
public void Writer()
{
using (System.IO.StreamWriter file = new System.IO.StreamWriter(path0save.Text))
{
DirectoryInfo directory = new DirectoryInfo(path0tb.Text);
DirectoryInfo[] directory0Arr = directory.GetDirectories();
foreach (DirectoryInfo dir in directory0Arr)
{
String Parent = Convert.ToString(dir.Parent);
String Name = Convert.ToString(dir.Name);
String Root = Convert.ToString(dir.Root);
file.Write(Parent);
file.Write(" || ");
file.WriteLine(Name);
file.WriteLine(Root);
}
}
}
public void Browser()
{
FolderBrowserDialog address = new FolderBrowserDialog();
if (address.ShowDialog() == System.Windows.Forms.DialogResult.OK)
path0tb.Text = address.SelectedPath;
}
public void Browser2()
{
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
path0save.Text = saveFileDialog1.FileName;
}
}
public Anime0Lister()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//_____________________________________________________________List
{
try
{
if (existing0file.Checked == true)
{
Appen();
}
else
{
Writer();
}
}
catch (Exception Fail)
{
MessageBox.Show(Fail.Message);
}
}
}
}
}
答案 0 :(得分:4)
像StreamWriter
这样的一次性对象应始终包含在Using Statement中,以确保即使在异常的情况下,也会关闭对象并释放其非托管资源以供重用。该语言为您提供了一个特定的声明,(Using Stetement),但在您的具体情况下,如果您使用StreamWriter
打开文件,则无法使用File.AppendAllText
,因为这方法尝试打开文件,并通过之前打开的StreamWriter
public void Appen()
{
using(System.IO.StreamWriter file = new System.IO.StreamWriter(path0save.Text, true))
{
DirectoryInfo directory = new DirectoryInfo(path0tb.Text);
DirectoryInfo[] directory0Arr = directory.GetDirectories();
foreach (DirectoryInfo dir in directory0Arr)
{
// No need for those Convert.ToString.
// The DirectoryInfo properties used are already strings.
file.Write(dir.Parent);
file.Write(" || ");
file.WriteLine(dir.Name);
file.WriteLine(dir.Root);
// or just in one line
//file.WriteLine(string.Concat(dir.Parent,
// " || ",
// dir.Name,
// Environment.NewLine,
// dir.Root));
}
}
}
相同的using语句应该应用于Writer方法...