该进程无法访问该文件,因为它正在被另一个进程使用(文件已创建,但不包含任何内容) [英] The process cannot access the file because it is being used by another process (File is created but contains nothing)

查看:590
本文介绍了该进程无法访问该文件,因为它正在被另一个进程使用(文件已创建,但不包含任何内容)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 使用System.IO; 

class test
{
public static void Main()
{

string path = @c:\mytext.txt ;

if(File.Exists(path))
{
File.Delete(path);
}


FileStream fs = new FileStream(path,FileMode.OpenOrCreate);
StreamWriter str = new StreamWriter(fs);
str.BaseStream.Seek(0,SeekOrigin.End);
str.Write(mytext.txt .........................);
str.WriteLine(DateTime.Now.ToLongTimeString()++ DateTime.Now.ToLongDateString());
string addtext =这一行被添加+ Environment.NewLine;
File.AppendAllText(path,addtext); //发生异常??????????
string readtext = File.ReadAllText(path);
Console.WriteLine(readtext);
str.Flush();
str.Close();

Console.ReadKey();
//System.IO.IOException:进程无法访问文件c:\mytext.txt,因为//被另一个进程使用。
//在System.IO .__ Error.WinIOError(Int32 errorCode,String maybeFullPath)

}
}


$ b $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ string path = @c:\mytext.txt;

if(File.Exists(path))
{
File.Delete(path);
}

{//考虑文件操作1
FileStream fs = new FileStream(path,FileMode.OpenOrCreate);
StreamWriter str = new StreamWriter(fs);
str.BaseStream.Seek(0,SeekOrigin.End);
str.Write(mytext.txt .........................);
str.WriteLine(DateTime.Now.ToLongTimeString()++
DateTime.Now.ToLongDateString());
string addtext =这一行被添加+ Environment.NewLine;
str.Flush();
str.Close();
fs.Close();
//关闭流然后单独您可以访问文件。
}

File.AppendAllText(path,addtext); //文件操作2

string readtext = File.ReadAllText(path); //文件操作3

Console.WriteLine(readtext);

在每个文件操作中,文件将被打开并且在打开之前必须关闭。就像操作1中的明智一样,您必须关闭进一步操作的文件流。


using System.IO;

class test
{
    public static void Main()
    {

        string path=@"c:\mytext.txt";

        if(File.Exists(path))
        {
            File.Delete(path);
        }


        FileStream fs=new FileStream(path,FileMode.OpenOrCreate);
        StreamWriter str=new StreamWriter(fs);
        str.BaseStream.Seek(0,SeekOrigin.End); 
        str.Write("mytext.txt.........................");
        str.WriteLine(DateTime.Now.ToLongTimeString()+" "+DateTime.Now.ToLongDateString());
        string addtext="this line is added"+Environment.NewLine;
        File.AppendAllText(path,addtext);  //Exception occurrs ??????????
        string readtext=File.ReadAllText(path);
        Console.WriteLine(readtext);
        str.Flush();
        str.Close();

        Console.ReadKey();
  //System.IO.IOException: The process cannot access the file 'c:\mytext.txt' because it is //being used by another process.
  // at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)

    }
}

解决方案

Try This

string path = @"c:\mytext.txt";

if (File.Exists(path))
{
    File.Delete(path);
}

{ // Consider File Operation 1
    FileStream fs = new FileStream(path, FileMode.OpenOrCreate);
    StreamWriter str = new StreamWriter(fs);
    str.BaseStream.Seek(0, SeekOrigin.End);
    str.Write("mytext.txt.........................");
    str.WriteLine(DateTime.Now.ToLongTimeString() + " " + 
                  DateTime.Now.ToLongDateString());
    string addtext = "this line is added" + Environment.NewLine;
    str.Flush();
    str.Close();
    fs.Close();
    // Close the Stream then Individually you can access the file.
}

File.AppendAllText(path, addtext);  // File Operation 2

string readtext = File.ReadAllText(path); // File Operation 3

Console.WriteLine(readtext);

In every File Operation, The File will be Opened and must be Closed prior Opened. Like wise in the Operation 1 you must Close the File Stream for the Further Operations.

这篇关于该进程无法访问该文件,因为它正在被另一个进程使用(文件已创建,但不包含任何内容)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆