0

I am trying to find the size of file and store the size in string fileSize = string.Empty;. for this i am saving the file with unique name and then reading finally deleting the file .

I am sometime getting error either in reading or deleting the file.

while reading i am getting this error

the process cannot access the file 'filename' because it is being used by another process. at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost) at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share)

while deleting i am getting this error

Access to the path 'filename' is denied. at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) at System.IO.File.InternalDelete(String path, Boolean checkHost)

my code is:

bool checksize(Outlook.Attachment attachment)
{
string currentTime = DateTime.Now.ToString("HHmmssff");
if (!Directory.Exists(GetConfigSettings("folderPath")))
{
Directory.CreateDirectory(GetConfigSettings("folderPath"));
}
attachment.SaveAsFile(GetConfigSettings("folderPath") + "\\"+ currentTime + "_" + attachment.FileName);


using (BinaryReader br = new BinaryReader(File.Open(GetConfigSettings("folderPath") + "\\"+ currentTime + "_" + attachment.FileName), FileMode.Open)))
{
fileSize = br.BaseStream.Length.ToString();
}

File.Delete(GetConfigSettings("folderPath") + "\\"+ currentTime + "_" + attachment.FileName));
//somecode
}

I am not getting why randomly it throws exception.

Dvyn Resh
  • 980
  • 1
  • 6
  • 14
  • 1
    Probably because [File locked after sending it as attachment](https://stackoverflow.com/questions/5191449/file-locked-after-sending-it-as-attachment) – Steve Aug 12 '19 at 08:26
  • you don't need a binaryReader, just use the stream that is returned by File.Open(...) – D.J. Aug 12 '19 at 08:38
  • Either your disposable objects (stream and reader) are not disposed quick enough or Outlook.Attachment.SaveAsFile(...) does not dispose correctly – D.J. Aug 12 '19 at 08:45
  • The attachment object has a method `Size` - can you use that instead? https://learn.microsoft.com/en-us/office/vba/api/outlook.attachment.size – Simon MᶜKenzie Aug 12 '19 at 09:38
  • @D.J. how to dispose stream and reader object, stream and reader are under using statement so they should dispose automatically. also `attachment.Dispose();` is not valid command as it is showing red line below Dispose. – Mohammad Sarfaraz Aug 12 '19 at 11:32
  • @mohdsarfaraz reader is in using-statment, the stream is not. also you do not need reader AND stream for your purpose. i didn't suggest 'attachment.Dispose()'. i meant that the method or class does not propery dispose stuff in its inner workings – D.J. Aug 12 '19 at 11:43
  • @D.J. i am not getting how to resolve this issue, can you please provide me the code or give idea how to do it. – Mohammad Sarfaraz Aug 12 '19 at 11:54
  • Perhaps your create-open-delete is being held back by a virus scanner or something. – CodeCaster Aug 12 '19 at 12:31
  • issue which i figured it out is that when saving big file it takes some time and i that particular time i am trying to read that file , that is why exception `the process cannot access the file 'filename' because it is being used by another process.` is thrown. now how to give time to save file and then only read the file , similar situation for delete also. – Mohammad Sarfaraz Aug 12 '19 at 19:37

2 Answers2

2

Instead of saving the attachment to a temporary file, opening this file using an irrelevant reader and then checking the reader's base stream's length, only to then delete the file again, use Outlook.Attachment.Size.

You can replace your entire method with this:

bool checksize(Outlook.Attachment attachment)
{
    filesize = attachment.Size.ToString();
    return true;
}

Also, the signature bool checksize doesn't really make sense, nor does storing the filesize in a class member, nor does storing it as a string, but that's not what your question was about.

CodeCaster
  • 147,647
  • 23
  • 218
  • 272
2

by adding wait in between save and read solved my issue.

attachment.SaveAsFile(GetConfigSettings("folderPath") + "\\"+ currentTime + "_" + attachment.FileName);
System.Threading.Thread.Sleep(100);
using (BinaryReader br = new BinaryReader(File.Open(GetConfigSettings("folderPath") + "\\"+ currentTime + "_" + attachment.FileName), FileMode.Open)))
{
fileSize = br.BaseStream.Length.ToString();
}