[英]How can I write to a file and remove the file afterwards in C#?
我需要创建一个文件,在文件中写一行文本,然后删除该文件并估计需要多长时间。
不幸的是,我遇到了两个问题,首先我无法在文件中写入文件,它成功创建了文件,但是没有写入任何文件。
其次,我无法删除该文件,因为它已被另一个进程使用。
请帮忙。
我已经尝试删除它一段时间了。
我也曾尝试将其包装在uses中,但无济于事。
写入文件的情况相同。 我什至更改了它,使文件以.txt结尾,但这没有任何区别。
public static void ProcessFile(string path)
{
string fullpath = path;
int lastBracket = path.LastIndexOf("\\");
// the filename does not contain .html so it can be named to .txt
string newFileName = path.Substring(lastBracket + 1, path.Length - lastBracket - 6) + " hrefcount.txt";
string newPath = Path.Combine(fullpath.Substring(0, lastBracket), newFileName);
Console.WriteLine(newPath);
int counter = 0;
foreach (var line in File.ReadAllLines(path))
{
if (line.Contains("href="))
{
counter++;
}
}
var fileCreated = File.CreateText(newPath);
fileCreated.WriteLine("The number of times href appears is " + counter);
Console.WriteLine();
File.Delete(newPath);
}
已创建文件,未写入任何文件,由于另一进程已使用而无法删除。
代替File.CreateText()
使用File.WriteAllText(path, content)
。 它写入文本,然后关闭文件,允许您在必要时将其删除
代替以下
var fileCreated = File.CreateText(newPath);
fileCreated.WriteLine("The number of times href appears is " + counter);
你可以写
File.WriteAllText(newPath, $"The number of times href appears is {counter}");
在这里参考文档
您的方法存在的问题是CreateText()
用于写入流。 但是对于您而言,这是没有必要的,因为您要立即将所有文本写入文件,并且该文本的大小很小。
我无法删除该文件,因为该文件已被其他进程使用。
创建后可能没有处置文件。 为此,您还应该使用FileStream.Dispose
方法:
File.Create(path).Dispose();
估计需要多长时间
您可以使用System.Diagnostics.Stopwatch
类来执行此操作:
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
/*
do the magic
*/
stopwatch.Stop();
Console.WriteLine(stopwatch.ElapsedMilliseconds);
您可以使用File.WriteAllText
方法而不是File.CreateText
来将文本写入文件:
File.WriteAllText(path, myText);
请记住,自.NET 4起,您也可以将此方法与array或List<T>
而不是字符串。
File.Create()
支持Dispose方法,该方法可帮助您释放该文件资源以执行进一步的操作
要对文件执行操作,请执行以下步骤:
创建文件并使用Dispose()
释放资源。
File.Create(newPath).Dispose();
或使用StreamWriter
创建文件并向其中写入文本。
using( StreamWriter sw = new StreamWriter(newPath, true)
{
sw.Write($"The number of times href appears is {counter}"); //Used string interpolation
}
StreamWriter调用Dispose()
函数以释放文件资源。
当Writer释放对文件的控制权时,您将不会遇到与我无法删除文件有关的问题, 因为该文件已被另一个进程使用。
现在,您可以使用以下方式删除文件:
MSDN: IDisposable.Dispose方法
执行与释放,释放或重置非托管资源相关的应用程序定义的任务。
错误的原因是您没有关闭并处理变量fileCreated 。 这是一个FileStream,在关闭并释放此变量之前,该文件对任何人都不可用,即使您自己的打开文件的代码也是如此。
所以要做的第一件事是
using (var fileCreated = File.CreateText(newPath))
{
fileCreated.WriteLine("The number of times href appears is " + counter);
}
using块可确保正确处理变量。
但是,您可以简化代码的其他部分
public static void ProcessFile(string path)
{
string folder = Path.GetDirectoryName(path);
string file = Path.GetFileName(path);
// Keep the first 6 characters from the source file?
string newFile = file.Substring(0, 6) + " hrefcount.txt";
string newPath = Path.Combine(folder, newFile);
// A single line to retrieve your counter thanks to IEnumerables and Linq
int counter = File.ReadLines(path).Count(x => x.Contains("href="));
// Create, but dispose also the file
using (var fileCreated = File.CreateText(newPath))
{
fileCreated.WriteLine("The number of times href appears is " + counter);
}
// Now you should be free to delete the file
File.Delete(newPath);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.