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.
Size
- can you use that instead? docs.microsoft.com/en-us/office/vba/api/outlook.attachment.size – Simon MᶜKenzieattachment.Dispose();
is not valid command as it is showing red line below Dispose. – Mohammad Sarfaraz