How to delete any dynamically created file from solution? - c#

I am creating a file into the solution's directory as below:
var targetPathToCreate = Path.Combine(HttpRuntime.AppDomainAppPath, "FolderName");
DirectoryInfo dirInfo = new DirectoryInfo(targetPathToCreate);
if (!dirInfo.Exists)
{
dirInfo.Create();
}
var filePath = Path.Combine(targetPathToCreate, "FileName");
System.IO.File.WriteAllBytes(filePath, bytesArray);
and further I am sending this document via email and then trying to delete this document.
For that I tried this code:
System.IO.File.Delete("FilePath");
but this line throws an exception:
The process cannot access the file because it is being used by another process
Is there any way to delete the files by overcoming the exception?

You can use garbage collector's WaitForPendingFinalizers function.
It will suspend the current thread until the thread that is processing the queue of finalizers has emptied that queue.
if (System.IO.File.Exists(filePath))
{
System.GC.Collect();
System.GC.WaitForPendingFinalizers();
System.IO.File.Delete(filePath);
}
If this doesn't work, you have to dispose your mail object.
mail.Attachments.Add(...);
smtpServer.Send(mail);
mail.Dispose();
System.IO.File.Delete(filePath);

I took a look and noticed that the error is possibly generated because your code System.IO.File.WriteAllBytes(filePath, bytesArray); may not be finished in its execution.
Here is a resource you can look at. You could also get the newly generated file's name by writing a new method that mails it and then deletes it, which could include something like this:
System.IO.FileInfo fi = new System.IO.FileInfo(#"C:\MyFiles\" + yourfilename + "'");
try
{
fi.Delete();
}

Related

StreamReader Could not find file (Because it is not being created)

I am trying to create a file for the further write to and read from.
I use Directory.CreateDirectory and File.Create but neither path nor file are being created.
On the Page which part I show here below I check if File exists, and if not, I create a File. On Second Page (that I dont show here) I add new lines to the file using StreamWrite. When saved, first Page comes to focus again and lists the content of the File(only one row in this study).
Here is my code for the part in question:
public async Task ReadFileAsync()
{
string directoryName = Path.GetDirectoryName(#"C:\Users\...\DataBase\");
Task.Run(async() => Directory.CreateDirectory(directoryName));
Task.Run(async() => File.Create(directoryName + "ProductsDatabase.txt"));
//code for reading from file
string path = (directoryName + "ProductsDatabase.txt");
using (StreamReader ProductsDatabaseRead = new StreamReader(File.OpenRead(path)))
{
ProductOneTextBlock.Text = ProductsDatabaseRead.ReadLine();
}
if (ProductOneTextBlock.Text == "")
{
ProductOneTextBlock.Text = "Nothing to show";
}
}
The file and folder are not being created.
I don't get any error.
I tried also different folders on the drive in case if there was READ ONLY folder in solution folder. No difference.
Anyone could help?
(I found many threads about this problem but here I cannot resolve it with none of the solutions.
Physical file is not being created.
When I attempt to write to it (from another page) I get error that the file could not be found(because it is not there indeed).
It seems that program loses itself somewhere between
Task.Run(async() => Directory.CreateDirectory(directoryName));
Task.Run(async() => File.Create(directoryName + "ProductsDatabase.txt"));
and:
using (StreamReader ProductsDatabaseRead = new StreamReader(File.OpenRead(path)))
{
ProductOneTextBlock.Text = ProductsDatabaseRead.ReadLine();
}
, as TextBlock is not being updated even if ProductsDatabaseRead is null.
If I put
ProductOneTextBlock.Text = "Nothing to show";
a the begining of the method, TextBlock gets updated.
SO, why the
using (StreamReader ProductsDatabaseRead = new StreamReader(File.OpenRead(path)))
does not work?
You're not waiting for Task.Run to complete. Your directory creation, file creation and attempt to open a "as you think newly created file" are out of order. That's why you're probably not able to open a file (it still does not exist at this point).
Task.Run returns a task that will be completed when the work is done. You need to wait for completion.
public void ReadFile()
{
string folderPath = #"C:\Users\patri\source\repos\DietMate\";
string fileName = "ProductsDatabase.txt";
string fullPath = Path.Combine(folderPath, fileName);
//insert code to check whether file exists.
// use Exists()
if (!Directory.Exists(folderPath))
{
Directory.CreateDirectory(folderPath);
File.Create(fullPath);
}
//if yes, follow with code below
//insert code for reading from file
using (StreamReader ProductsDatabaseRead = new StreamReader(fullPath))
{
ProductTest.Text = ProductsDatabaseRead.ReadLine();
}
}

File in use error when deleting a folder

I am trying to delete a folder but am getting the following error message:
The process cannot access the file .it is being used by another process.
string target_dir="D:\\projectpath\\page";
if (Directory.Exists(target_dir))
Directory.Delete(target_dir, false);
How can I resolve this error?
It looks like the file is locked by some other process. This could happen if when reading/writing to it you forgot to dispose the stream reader/writer and you leaked the unmanaged handler to the file.
For example if you used the following code to read from the file:
StreamReader reader = new StreamReader(fileName);
string contents = reader.ReadToEnd();
and you never release the reader, the file will be locked. The proper way is to wrap IDisposable resources such as Streams and StreamReaders in using statements:
using (StreamReader reader = new StreamReader(fileName))
{
string contents = reader.ReadToEnd();
}
If on the other hand the file is locked by some other external process to your application then there's very little you could do about it, other than killing this process.
I think on the surface, your problem should be apparent: the file is in use by something else, so you can't delete the directory it resides in. If there was a way to "force delete" the file, it could cause other programs to crash. I'd recommend catching the error and either logging it or displaying it to the user, so they can decide if they really want to delete the in-use file.
If you MUST delete the file, you could take a look at:
Using C#, how does one figure out what process locked a file?
And once you know what the process is, you can then kill it, which should free up the file. Again, this isn't a good practice and should only be used in exceptional circumstances.
To delete the diectory you must have the correct Permissions.
var target_dir = "D:\\projectpath\page";
var isWriteAccess = false;
try
{
var collection = Directory.GetAccessControl(target_dir)
.GetAccessRules(true, true, typeof(System.Security.Principal.NTAccount));
if (collection.Cast<FileSystemAccessRule>().Any(rule => rule.AccessControlType == AccessControlType.Allow))
{
isWriteAccess = true;
}
}
catch (UnauthorizedAccessException ex)
{
isWriteAccess = false;
}
catch (Exception ex)
{
isWriteAccess = false;
}
if (!isWriteAccess)
{
MessageBox.Show("no access to directory.");
// Handle here close and kill the blocking process
}
else
{
Directory.Delete(target_dir, false);
}
}

File locked after sending it as attachment

I am sending a file as an attachment:
// Create the file attachment for this e-mail message.
Attachment data = new Attachment(filePath, MediaTypeNames.Application.Octet);
// Add time stamp information for the file.
ContentDisposition disposition = data.ContentDisposition;
disposition.CreationDate = System.IO.File.GetCreationTime(filePath);
disposition.ModificationDate = System.IO.File.GetLastWriteTime(filePath);
disposition.ReadDate = System.IO.File.GetLastAccessTime(filePath);
// Add the file attachment to this e-mail message.
message.Attachments.Add(data);
And then I want to move the file to another folder, however when I try to do this
try
{
//File.Open(oldFullPath, FileMode.Open, FileAccess.ReadWrite,FileShare.ReadWrite);
File.Move(oldFullPath, newFullPath);
}
catch (Exception ex)
{
}
Its throwing an exception that the file is already being used in another process. How I can unlock this file so that it can be moved to this location?
Disposing your message will fix this for you. Try calling Dispose on your message before moving the file, like so:
message.Dispose();
File.Move(...)
When disposing MailMessage, all locks and resources are released.
You need to take advantage of "using" keyword:
using(Attachment att = new Attachment(...))
{
...
}
That's because "using" ensures IDisposable.Dispose method is called at the end of code block execution.
Whenever you use some class that's managing some resource, check if it's IDisposable and then use "using" block and you won't need to care about manually disposing calling IDisposable.Dispose.
Attachments are IDisposable and should be disposed of correctly after they have been sent to release the lock on the file
In order to prevent this file lock from happening, you could use using as this will dispose of the object automatically:
using (Attachment data = new Attachment(filePath, MediaTypeNames.Application.Octet))
{
// Add time stamp information for the file.
ContentDisposition disposition = data.ContentDisposition;
disposition.CreationDate = System.IO.File.GetCreationTime(filePath);
disposition.ModificationDate = System.IO.File.GetLastWriteTime(filePath);
disposition.ReadDate = System.IO.File.GetLastAccessTime(filePath);
// Add the file attachment to this e-mail message.
message.Attachments.Add(data);
// Add your send code in here too
}
Here's another way to dispose of the Attachments, once you've finished with them...
// Prepare the MailMessage "mail" to get sent out...
await Task.Run(() => smtp.Send(mail));
// Then dispose of the Attachments.
foreach (Attachment attach in mail.Attachments)
{
// Very important, otherwise the files remain "locked", so can't be deleted
attach.Dispose();
}
A shared similar problem. I had to use image.dispose() on a given image before I could do something with the file that the image was created from.

Azure : The process cannot access the file "" because it is being used by another process

I am trying to get a matlab-compiled exe running on Azure cloud, and for that purpose need to get a v78.zip onto the local storage of the cloud and unzip it, before I can try to run an exe on the cloud. The program works fine when executed locally, but on deployment gives an error at line marked below in the code. The error is :
The process cannot access the file 'C:\Resources\directory\cc0a20f5c1314f299ade4973ff1f4cad.WebRole.LocalStorage1\v78.zip' because it is being used by another process.
Exception Details: System.IO.IOException: The process cannot access the file 'C:\Resources\directory\cc0a20f5c1314f299ade4973ff1f4cad.WebRole.LocalStorage1\v78.zip' because it is being used by another process.
The code is given below:
string localPath = RoleEnvironment.GetLocalResource("LocalStorage1").RootPath;
Response.Write(localPath + " \n");
Directory.SetCurrentDirectory(localPath);
CloudBlob mblob = GetProgramContainer().GetBlobReference("v78.zip");
CloudBlockBlob mbblob = mblob.ToBlockBlob;
CloudBlob zipblob = GetProgramContainer().GetBlobReference("7z.exe");
string zipPath = Path.Combine(localPath, "7z.exe");
string matlabPath = Path.Combine(localPath, "v78.zip");
IEnumerable<ListBlockItem> blocklist = mbblob.DownloadBlockList();
BlobStream stream = mbblob.OpenRead();
>> FileStream fs = File.Create(matlabPath); (Exception occurs here)
It'll be great help if someone could tell me where I'm going wrong.
CloudBlob's are not IDisposable, so you don't need a using statement. (They're just references, so they don't allocate any resources that need to be released.)
You should, however, probably have a using() block around the FileStream. Are you sure it's being closed?
Where is this code running? Is it run only once?
BTW, you can just do "container.GetBlobRefence("foo").DownloadToFile(matlabPath);" (anticipating what you're about to do with that file handle).
Add a using clause around most of this. You have a file handle to your zip file hanging around. When the using goes out of scope, so will the file reference.
using(CloudBlob mblob = GetProgramContainer().GetBlobReference("v78.zip"))
{
CloudBlockBlob mbblob = mblob.ToBlockBlob;
CloudBlob zipblob = GetProgramContainer().GetBlobReference("7z.exe");
string zipPath = Path.Combine(localPath, "7z.exe");
string matlabPath = Path.Combine(localPath, "v78.zip");
IEnumerable<ListBlockItem> blocklist = mbblob.DownloadBlockList();
BlobStream stream = mbblob.OpenRead();
}
FileStream fs = File.Create(matlabPath);
Try fs.close() once you are done with it.

C# Why is my code throwing a io.system.directorynotfound?

Why would the code below throw a io.system.directorynotfound exception? I can't recreate the problem myself but another user of my code does see it, any ideas why?
Thanks
try
{
//create path
string strAppData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData).ToString() + "\\MyApp\\Data\\logs";
//check path exists
if (!System.IO.File.Exists(strAppData))
{
System.IO.Directory.CreateDirectory(strAppData);
}
System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(strAppData);
int count = dir.GetFiles().Length;
if (count > 100)
{
string[] files = System.IO.Directory.GetFiles(strAppData);
foreach (string file in files)
{
System.IO.File.Delete(file);
}
}
this.fileName = fileName;
// delete the file if it exists
if (File.Exists(fileName))
{
//delete the file
File.Delete(fileName);
}
// write the data to the file
fs = File.OpenWrite(fileName);
sWriter = new StreamWriter(fs);
sWriter.WriteLine(headerText);
sWriter.Flush();
sWriter.Close();
}
catch (Exception exp)
{
throw new Exception(exp.Message);
}
Have you tried using System.IO.Directory.Exists rather than System.IO.File.Exists when checking to see if the path exists?
You're checking for the existence of a directory using System.IO.File rather than System.IO.Directory. It probably works on your machine because that directory already exists, and so the check doesn't matter.
Either way, you need to remember that the file system is volatile. Rather than checking existence, try to open the resource and handle the exception when it fails.
Check that the directory exists, not the file...
Although you're checking it, and creating it if it doesn't exist. You don't know if they have privelages to create the directory. So your Directory.CreateDirectory call may well be failing too and then sub-sequently the rest of the code will fail
http://msdn.microsoft.com/en-us/library/system.io.file.exists.aspx
"Remarks
The Exists method should not be used for path validation, this method merely checks if the file specified in path exists. Passing an invalid path to Existsl returns false. "
That is your error right there. Your validation does not ensure that the path to the file exists

Categories