C
C#ā€¢14mo ago
c0nstant

Cannot access file because it's being used by another process

I am getting the following error when I try to run my program: System.IO.IOException: 'The process cannot access the file because it is being used by another process.' I googled around and most suggestions want you to close the file stream when you are done with a file however the files that I am triyng to rename don't have a close method. Here is my code:
var output = @"E:\demo.txt";
var ext = RandomStringNS(12) + " - REST Database";
Microsoft.VisualBasic.FileIO.FileSystem.RenameFile(output, ext);
var output = @"E:\demo.txt";
var ext = RandomStringNS(12) + " - REST Database";
Microsoft.VisualBasic.FileIO.FileSystem.RenameFile(output, ext);
So when I run this on a directory, it works on the first and sometimes the second file until something gets in the way. So what I need to do is force my program to rename files. Any solutions to this?
5 Replies
Rist
Ristā€¢14mo ago
You sure you're not reading the files you just renamed while iterating over the directory? And that you don't have any files open in another program?
c0nstant
c0nstantā€¢14mo ago
I am iterating through two my of disks and then backing up all files:
string[] diskList = {"D:\\", "E:\\"};
foreach (string item in diskList)
{
string[] files = EnumerateFilesRecursive(item).ToArray();
foreach (string file in files)
{
var key = "my weak password";
var output = file;
Console.WriteLine(FileEncryptor.EncryptAsync(file, file, key));

var ext = RandomStringNS(12) + " - REST Database";
Microsoft.VisualBasic.FileIO.FileSystem.RenameFile(output, ext);

}
}
string[] diskList = {"D:\\", "E:\\"};
foreach (string item in diskList)
{
string[] files = EnumerateFilesRecursive(item).ToArray();
foreach (string file in files)
{
var key = "my weak password";
var output = file;
Console.WriteLine(FileEncryptor.EncryptAsync(file, file, key));

var ext = RandomStringNS(12) + " - REST Database";
Microsoft.VisualBasic.FileIO.FileSystem.RenameFile(output, ext);

}
}
The encrypt method here does have Close(); implemented like at the end of it, I found the implementation from here so I just added close methods at the end of the method: https://stackoverflow.com/questions/17627326/aes-256-file-encryption-c-sharp I was thinking that maybe the Microsoft's RenameFile method has a close option or maybe it opens the file and then I can close it somehow
Rist
Ristā€¢14mo ago
You're not awaiting it though and it's async
c0nstant
c0nstantā€¢14mo ago
Yes, it is, so what should I do here? Yeah, kind of... šŸ˜› but no serriously, how do I do this? I will come back with a solution then šŸ˜¦
Scratch
Scratchā€¢14mo ago
!ban 899204088112230421 developing ransomware
Want results from more Discord servers?
Add your server
More Posts
print out all pairs of natural numbers whose sum isequal to the entered number nWrite an algorithm that prints out all pairs of natural numbers whose sum is equal to the entered nuā” "BLAZOR102: The scoped css file '~\Layout.cshtml.css' was defined but..." How do I fix this error?I've been messing around with ASP.NET and trying to make a new web app, originally I was having issuā” Unity 3D Urp ProblemsDoes anyone know why when i make a 3d urp project, nothing shows up on my camera? The camera is lookāœ… im new and i dont get thisi get error cs 1525 unexpected symbol on code line 8 and 16āœ… `DllImport` on a dll name only known at runtimeI have a bunch of native dlls that have the same interface (they export the same functions with idenā” Some theory questions I'm unsure about :)I just have a few questions that I'm unsure about when I was revising for an exam. Sorry if some areāœ… [SOLVED] Need help with converting async method to a 'normal' methodI tried a few solutions to be able to call an async method from my Main method but those have failedāœ… Help me pleaseI am trying to do a project using asp.net identity. I am following n-layer mvc architecture, keepingā” The certificate chain was issued by an authority that is not trusted.Whenever I run the `Update-Database` command, I encounter an error related to the SSL certificate chā” Auto Formatting in Visual StudioEverytime I make a new line or use a ; Visual Studio auto formats the lines as shown and I was wonde