File Being Used by Another Process After Using File.Create()

File being used by another process after using File.Create()

The File.Create method creates the file and opens a FileStream on the file. So your file is already open. You don't really need the file.Create method at all:

string filePath = @"c:\somefilename.txt";
using (StreamWriter sw = new StreamWriter(filePath, true))
{
//write to the file
}

The boolean in the StreamWriter constructor will cause the contents to be appended if the file exists.

"File is being used by another process" after File.Create()

The problematic code/line is this

If Not File.Exists("./PalletQueue/Printer1" & ".txt") Then
File.Create("./PalletQueue/Printer1" & ".txt")
End If

File.Create returns a FileStream, that you need to close, if you want to write later to that file.
Changing your code to the following should solve your problem.

If Not File.Exists("./PalletQueue/Printer1" & ".txt") Then
Dim file as FileStream = File.Create("./PalletQueue/Printer1" & ".txt")
file.Close()
End If

The process cannot access the file because it is being used by another process. File.Create method

The error message is unhelpful because the most frequent cause of this error that most programmers experience is when their own process is the cause of the blockage, as it is here. So the error message blaming "another process" is incorrect.

File.Create returns a FileStream; an object that, in common with other streams, is IDisposable - you should dispose of it when you no longer need it. This may be as simple as:

string metaDataFilePath = Path.Combine(path, Metadata);
if (!File.Exists(path))
File.Create(path).Dispose();

File being used by another process after using File.AppendAllText()

my code is having an race condition, and this is because the API is being call by more than one user at each given time, even that

System.IO.File.AppendAllText(FilePath + @"\" + "FileNAme" + DateTime.Now.ToString("dd-MM-yyyy") + ".txt", DataToBeSave + Environment.NewLine);

will close after the Operation, it still need time do its work and only one connection can be open at each give time so the thread need to be lock and that can be done by

private static Object thisLock = new Object();
lock (thisLock)
{
System.IO.File.AppendAllText(FilePath + @"\" + "DandB" + DateTime.Now.ToString("dd-MM-yyyy") + ".txt", DataToBeSave + Environment.NewLine);
}

Thanks to Abydal

C# File being used by another process

Assuming you don't need access to this stream outside the context of this method, I'd refactor your code to this:

var filePath = Path.Combine("logs", _logFile);

using (var sw = File.AppendText(filePath))
{
//Do whatever writing to stream I want.
sw.WriteLine(DateTime.Now.ToString() + ": test log entry");
}

This way, no matter what happens inside the using block, you know the file will be closed so you can use it again later.

Note that File.AppendText will create the file if it doesn't already exist, so File.Create is not needed.

C# cannot access file immediately after creating it

Just use StreamWriter with param append = true. It'll create the file if needed.

using (StreamWriter sw = new StreamWriter(_filePath, true, Encoding.Default))
{
sw.WriteLine("blablabla");
}

Creating txt file cannot access file being used by another process

When creating the file you're mistakenly leaving it open which is why you can't access it next time and receive an error. You must call .Dispose() after you've created it to let go of the reference to the file like this:

File.Create(AppDomain.CurrentDomain.BaseDirectory + "OnStart.txt").Dispose();

If you want to keep creating files then they'll need a different name each time. You could keep a global variable to keep track of this or possibly pass a value into the Write method.

Global variable method

// Keeps track of the last file number with this global variable
private int fileCount;

public void OnDebug()
{
OnStart(null);
}

protected override void OnStart(string[] args)
{
fileCount = 0; // Or whatever you want to start with
Timer t = new Timer(WriteTxt, null, 0, 5000);
}

public static void WriteTxt(Object i)
{
// Creates the file name eg: OnStart1.txt
var fileName = string.Format("OnStart{0}.txt", fileCount);

// Use Path.Combine to make paths
var filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, fileName);

// Create the file
File.Create(filePath).Dispose();

// Adds 1 to the value
fileCount++;
}

Results:

OnStart0.txt

OnStart1.txt

OnStart2.txt

...

...

OnStart4999.txt



Related Topics



Leave a reply



Submit