Overwrite an file which is currently open and it use in c# - c#

I am using C# to create a GUI which has a logging feature. When I create the log file, I provide it a filename. But I have the following issue. For instance, if the file is already open in the background on my desktop and I want to name my filename as the same, I get the exception message as following:
The process cannot access the file 'C:\blah\blah\mytest.csv' because it is being used by another process.
I am using the following piece of code to check if the file exists and using the streamwriter to create the new file.
if (FileUtils.Exists(fileName))
{
if (!FileUtils.Delete(fileName))
{
cout<< Error replacing log file, it might be used by another process";
}
}
//Exception handling
public static bool Delete(string myfiletowrite)
{
try
{
File.Delete(myfiletowrite);
return true;
}
catch (IOException)
{
return false;
}
}
myfile= new StreamWriter(myfiletowrite, true); //exception occurs here when I try to write to myfiletowrite which is currently open
I am unsure on how to close this file which is open in the background so I could write to this file. I would appreciate it someone could help me with this.

If the other application did not explicitely allow it (via Share mode), it is not possible to open the file for writing.
If the other application is under your control, you have some options (open the file with FileShare.Write, implement some communication protocol between the two applications, use a common service for accessing the file, ...).
Best option would be to choose a different file name.

Related

System.IO.File.Delete throws "The process cannot access the file because it is being used by another process"

Every time I save a file and delete it right away using the function below, I keep getting this error message: "System.IO.IOException: The process cannot access the file because it is being used by another process".
Waiting for a couple of minutes or closing visual studio seems to only unlock the files that you uploaded previously.
public static bool DeleteFiles(List<String> paths)
{ // Returns true on success
try
{
foreach (var path in paths)
{
if (File.Exists(HostingEnvironment.MapPath("~") + path))
File.Delete(HostingEnvironment.MapPath("~") + path);
}
}
catch (Exception ex)
{
return false;
}
return true;
}
I think that the way I'm saving the files may cause them to be locked. This is the code for saving the file:
if (FileUploadCtrl.HasFile)
{
filePath = Server.MapPath("~") + "/Files/" + FileUploadCtrl.FileName;
FileUploadCtrl.SaveAs(filePath)
}
When looking for an answer I've seen someone say that you need to close the streamReader but from what I understand the SaveAs method closes and disposes automatically so I really have no idea whats causing this
After some testing, I found the problem. turns out I forgot about a function I made that was called every time I saved a media file. the function returned the duration of the file and used NAudio.Wave.WaveFileReader and NAudio.Wave.Mp3FileReader methods which I forgot to close after I called them
I fixed these issues by putting those methods inside of a using statement
Here is the working function:
public static int GetMediaFileDuration(string filePath)
{
filePath = HostingEnvironment.MapPath("~") + filePath;
if (Path.GetExtension(filePath) == ".wav")
using (WaveFileReader reader = new WaveFileReader(filePath))
return Convert.ToInt32(reader.TotalTime.TotalSeconds);
else if(Path.GetExtension(filePath) == ".mp3")
using (Mp3FileReader reader = new Mp3FileReader(filePath))
return Convert.ToInt32(reader.TotalTime.TotalSeconds);
return 0;
}
The moral of the story is, to check if you are opening the file anywhere else in your project
I think that the problem is not about streamReader in here.
When you run the program, your program runs in a specific folder. Basically, That folder is locked by your program. In that case, when you close the program, it will be unlocked.
To fix the issue, I would suggest to write/delete/update to different folder.
Another solution could be to check file readOnly attribute and change this attribute which explained in here
Last solution could be using different users. What I mean is that, if you create a file with different user which not admin, you can delete with Admin user. However, I would definitely not go with this solution cuz it is too tricky to manage different users if you are not advance windows user.

Create .txt file and open in Notepad without saving

I need to create a .txt file in C# .
Is there a way (and how to do?) to create a file and open it in Notepad, but without saving in somewhere first? The user would save it after he checks it.
No not really, i've seen some programs that do this like so, but its not ideal:
Create a temporary file, most programs use the temp directory
you get there by using %temp% or C:\Users\{username}\AppData\Local\Temp so e.g. File.Create(#"C:\Users\{username}\AppData\Local\Temp\myTempFile.Txt")
Open the file with notepad. (File.Open(#"C:\Users\{username}\AppData\Local\Temp\myTempFile.Txt"))
The user makes the change and saves
Your program checks the file to see if any edits were made.
if any edits have been made, you can prompt the user to save the file to the actual location.
e.g. !string.IsNullOrWhiteSpace(File.ReadAllText(#"C:\Users\{username}\AppData\Local\Temp\myTempFile.Txt"))
If the user wants to save the file, the file gets copied to the real location
File.Copy(#"C:\Users\{username}\AppData\Local\Temp\myTempFile.Txt", #"c:\myRealPath\MyRealFileName.txt"
I created a way (I don't know if it already exists), using System.Diagnostics and System.Reflection libraries. My code is:
File.WriteAllText($#"{Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)}\exampleDoc.txt", "information inside file");
while (!File.Exists($#"{Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)}\exampleDoc.txt")) { }
Process.Start($#"{Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)}\exampleDoc.txt");
while (Process.GetProcesses().Where(prc => { try { return prc.MainWindowTitle.Contains("exampleDoc.txt"); } catch { return false; } }).Count() == 0) { }
File.Delete($#"{Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)}\exampleDoc.txt");

Streamwriter crashes when writing to existing file

Here is the code I have that writes to a couple of files:
StreamWriter writer = new StreamWriter(#"C:\TotalStock\data\points\" + stockName.ToUpper() + ".txt");
for(int i = 0; i < lines; i++)
{
writer.WriteLine(lineData[i]);
postGui.Send((object state) =>
{
progressBar2.PerformStep();
}, null);
}
writer.Close();
When I delete the text files and run the code there is no issue, but then when I close the application and run it once more the program gives me the following error. What is it that causes this error and what can I do to stop it?
Error:
An unhandled exception of type 'System.IO.IOException' occurred in mscorlib.dll
Additional information: The process cannot access the file 'C:\TotalStock\data\points\IBM.txt' because it is being used by another process
As marc_s pointed out, the error occurs because the file you are trying to edit is opened by another application. If you are certain that you don't have the file opened in any other editor/viewer, the problem may the program itself.
If several instances of the same code run concurrently and require access to the same file, say in a multithread environement.
Does another component in your application read from the text file while you are trying to write to the file?
Does your application hang, and the second instance requires the same file?
Do you run the same tests each time?
Probably the problem is that you open the file but do not close it when you exit the program. i see you are using StreamWriter to write the data to the file.
might by you get an exception and therefor does not close the file. when playing with files you should always do:
try
{
// Your code with files
}
catch
{
}
finally
{
writer.Close();
}
other reasons might be that you are using some other File/Stream/etc. please be sure that you close all the members that need to be closed before close the program.
please share all your code if you want us to check if you forgot something else
as Sayse Is saying another way to make sure you close your writers is a using statement:
using(StreamWriter writer = new StreamWriter(#"C:\TotalStock\data\points\" + stockName.ToUpper() + ".txt");)
{
// Your code of playing with files
}

what process is preventing my file from getting deleted in C#

I have a C# single thread application that creates a file. Uses that file and then deletes it. Some times the app has trouble deleting that file. The error I get is:
"The process cannot access the file --file path and file name-- because it is being used by another process."
How can I find out what process has a hold on this file and how can I make that process to let go so that the file can be deleted.
This thing rocks for that very "gotcha".
http://technet.microsoft.com/en-us/sysinternals/bb896645.aspx
Process Monitor v3.05
It has a "Filter" submenu so you can fine tune it to the file that is locked.
You need to post the relevant code so we can see.
It is however always important to make sure that your app close the file that it has opened.
usually something like this will ensure that:
using(var f = File.OpenRead("myfile")) {
...
}
or the equivalent:
try {
var f = File.OpenRead("myfile");
} finally {
f.close()
}
Make sure that you are closing file before delete.
if you are using StreamWriter class make sure that you are closing with its variable
Ex. StreamWriter sw = new StreamWriter();
// some writing operation
sw.Close();

Deleting a temp file that is open c#

i have written some pdf files to a temp directory and these get displayed as a thumbnail that the user can view. when i close my form i clean up all of the files in the temp directory.
If however the user has one of the thumbnails open and then closes my application - it deletes the files and then throws an exception because the pdf is open in another process and cant be cleaned up.
I guess this is a shocking programming decision by me, but i am still a novice! How should i account for this in my code?
Thanks
You can detect if the file is in use by using code similar to below, then use that to warn the user that a file can't be deleted.
Unfortunately you can't delete a file that is in use.
public static bool IsFileInUse(string pathToFile)
{
if (!System.IO.File.Exists(pathToFile))
{
// File doesn't exist, so we know it's not in use.
return false;
}
bool inUse = false;
System.IO.FileStream fs;
try
{
fs = System.IO.File.Open(pathToFile, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Read, System.IO.FileShare.None);
fs.Close();
}
catch (System.IO.IOException ex)
{
string exMess = ex.Message;
inUse = true;
}
return inUse;
}
You should catch that exception (in catch block you can inform user to close that file or it will not be deleted), and if the temp directory is yours you can try to delete it when application starts (or when it ends again), if its windows temp directory, then it does not matter that much
Tools like File Unlocker can release a file. However I think this could make programs depending on the file crash...
Maybe you can look up how they unlock files or manage to execute the unlocker via Process.Start to unlock your file and delete it.
However if it's you blocking the file you should try and fix this in your programm. Maybe you should dispose all loaded files (filestreams etc) before trying to clean it up.

Categories

Resources