0
votes

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.

2
you don't need a binaryReader, just use the stream that is returned by File.Open(...)D.J.
Either your disposable objects (stream and reader) are not disposed quick enough or Outlook.Attachment.SaveAsFile(...) does not dispose correctlyD.J.
The attachment object has a method Size - can you use that instead? docs.microsoft.com/en-us/office/vba/api/outlook.attachment.sizeSimon MᶜKenzie
@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

2 Answers

2
votes

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.

1
votes

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();
}