繁体   English   中英

该进程无法访问文件'D:.txt',因为它正在由c#中的另一个进程使用

[英]The process cannot access the file 'D:.txt' because it is being used by another process in c#

我有一个pdf文件,我正在尝试使用itext在c#中读取和写入文本文件。现在我已经创建了一个文本文件并尝试将我的pdf值写入其中,但是它给出了以下错误。

该进程无法访问文件'D:\\ 9008028901.txt',因为该文件正在被另一个进程使用。

这是我在C#中的代码。

public bool ExtractText(string inFileName, string outFileName)
{
    StreamWriter outFile = null;
    try
    {
       // Create a reader for the given PDF file
       PdfReader reader = new PdfReader(inFileName);
       outFile = File.CreateText(outFileName);

       outFile = new StreamWriter(outFileName, false, System.Text.Encoding.UTF8);

这是在我的代码文本文件中创建的,但是我无法在其中写任何东西。我的文本文件为空。 请帮我..

File.CreateText方法返回使文件保持打开状态的StreamWriter对象。 它没有关闭,然后您尝试通过new StreamWriter调用再次打开文件,从而遇到使用中的错误。 要解决此问题,您需要关闭第一个StreamWriter

outFile = File.CreateText(outFileName);
outFile.Close();
outFile = new StreamWriter(outFileName, false, System.Text.Encoding.UTF8);

总体而言,这似乎有点浪费。 一次创建StreamWriter实例将更加高效

outFile = new StreamWriter(outFile, false, System.Text.Encoding.UTF8);

File.CreateText方法似乎在这里是不必要的。 如果文件不存在, StreamWriter将创建它

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM