473,757 Members | 2,081 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem accessing uploaded file.

When using SmtpClient and sending a message with an attached file using
SendAsync(...), if I add File.Delete(pat hToAttachedFile ) in the
SendCompleted event handler I get a file in use exception. Is there a
way to overcome this problem?

Thanks,
Nick Z.
Dec 5 '05 #1
5 2814
Nick,

It seems you are generating a temporary file which is attached to the
email, and then mailed.

What I would do here is that when creating the file, make a call to
CreateFile through the P/Invoke layer. When you call this, pass
FILE_ATTRIBUTE_ TEMPORARY and FILE_FLAG_DELET E_ON_CLOSE.
FILE_ATTRIBUTE_ TEMPORARY will prevent the file from being written to the
disk if sufficient cache memory is available. FILE_FLAG_DELET E_ON_CLOSE
will cause the file to be deleted when it is closed.

Once you have that, create your FileStream (passing the handle returned
from CreateFile), and pass that to the constructor for the Attachment that
you add to the email.

That way, once the file is sent, and you call dispose on the Message,
the file will be deleted automatically.

The reason this happens is for one of two reasons. First could be that
you are using the constructor for Attachment which is taking the filename as
a string. When this happens, it opens the file for read access, and only
allows read operations to be performed on the file while the stream is open.
The stream is still open when the event is fired.

The second could be that you are not setting the share mode on your
FileStream to the appropriate value when creating it. You could fix it on
this level, and then manually delete the file, but I think letting the OS do
it is better.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Nick Z." <pa*****@gmail. com> wrote in message
news:Z2******** ********@fe09.l ga...
When using SmtpClient and sending a message with an attached file using
SendAsync(...), if I add File.Delete(pat hToAttachedFile ) in the
SendCompleted event handler I get a file in use exception. Is there a way
to overcome this problem?

Thanks,
Nick Z.

Dec 5 '05 #2
Thanks for the info. The problem is I only used File.Delete() as an
example, I actually want to write to the file. Sorry for not clarifying
that. In other words, write to file -> send it as attachment -> write to
the same file again once the attachment is sent. It seems the file is
never released by the SmtpClient or MailMessage classes.

Thanks again,
Nick Z.

Nicholas Paldino [.NET/C# MVP] wrote:
Nick,

It seems you are generating a temporary file which is attached to the
email, and then mailed.

What I would do here is that when creating the file, make a call to
CreateFile through the P/Invoke layer. When you call this, pass
FILE_ATTRIBUTE_ TEMPORARY and FILE_FLAG_DELET E_ON_CLOSE.
FILE_ATTRIBUTE_ TEMPORARY will prevent the file from being written to the
disk if sufficient cache memory is available. FILE_FLAG_DELET E_ON_CLOSE
will cause the file to be deleted when it is closed.

Once you have that, create your FileStream (passing the handle returned
from CreateFile), and pass that to the constructor for the Attachment that
you add to the email.

That way, once the file is sent, and you call dispose on the Message,
the file will be deleted automatically.

The reason this happens is for one of two reasons. First could be that
you are using the constructor for Attachment which is taking the filename as
a string. When this happens, it opens the file for read access, and only
allows read operations to be performed on the file while the stream is open.
The stream is still open when the event is fired.

The second could be that you are not setting the share mode on your
FileStream to the appropriate value when creating it. You could fix it on
this level, and then manually delete the file, but I think letting the OS do
it is better.

Hope this helps.

Dec 5 '05 #3
Nick,

It should be released when you call the Dispose method on the
MailMessage class. MailMessage implements IDiposable, and you should make
sure you dispose of it correctly. The best way to do this, since you are
sending asychronously, is to send the MailMessage as the state when you send
it.

Then, in your event handler, call Dispose on the MailMessage instance
that you sent. When that is done, you will have to re-open the file for
writing, since the stream will have been closed down by calling Dispose on
the MailMessage instance.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Nick Z." <pa*****@gmail. com> wrote in message
news:sO******** ********@fe12.l ga...
Thanks for the info. The problem is I only used File.Delete() as an
example, I actually want to write to the file. Sorry for not clarifying
that. In other words, write to file -> send it as attachment -> write to
the same file again once the attachment is sent. It seems the file is
never released by the SmtpClient or MailMessage classes.

Thanks again,
Nick Z.

Nicholas Paldino [.NET/C# MVP] wrote:
Nick,

It seems you are generating a temporary file which is attached to the
email, and then mailed.

What I would do here is that when creating the file, make a call to
CreateFile through the P/Invoke layer. When you call this, pass
FILE_ATTRIBUTE_ TEMPORARY and FILE_FLAG_DELET E_ON_CLOSE.
FILE_ATTRIBUTE_ TEMPORARY will prevent the file from being written to the
disk if sufficient cache memory is available. FILE_FLAG_DELET E_ON_CLOSE
will cause the file to be deleted when it is closed.

Once you have that, create your FileStream (passing the handle
returned from CreateFile), and pass that to the constructor for the
Attachment that you add to the email.

That way, once the file is sent, and you call dispose on the Message,
the file will be deleted automatically.

The reason this happens is for one of two reasons. First could be
that you are using the constructor for Attachment which is taking the
filename as a string. When this happens, it opens the file for read
access, and only allows read operations to be performed on the file while
the stream is open. The stream is still open when the event is fired.

The second could be that you are not setting the share mode on your
FileStream to the appropriate value when creating it. You could fix it
on this level, and then manually delete the file, but I think letting the
OS do it is better.

Hope this helps.


Dec 5 '05 #4
That did it!

Thank you,
Nick Z.

Nick Z. wrote:
Thanks for the info. The problem is I only used File.Delete() as an
example, I actually want to write to the file. Sorry for not clarifying
that. In other words, write to file -> send it as attachment -> write to
the same file again once the attachment is sent. It seems the file is
never released by the SmtpClient or MailMessage classes.

Thanks again,
Nick Z.

Nicholas Paldino [.NET/C# MVP] wrote:
Nick,

It seems you are generating a temporary file which is attached to
the email, and then mailed.

What I would do here is that when creating the file, make a call
to CreateFile through the P/Invoke layer. When you call this, pass
FILE_ATTRIBUTE_ TEMPORARY and FILE_FLAG_DELET E_ON_CLOSE.
FILE_ATTRIBUTE_ TEMPORARY will prevent the file from being written to
the disk if sufficient cache memory is available.
FILE_FLAG_DELET E_ON_CLOSE will cause the file to be deleted when it is
closed.

Once you have that, create your FileStream (passing the handle
returned from CreateFile), and pass that to the constructor for the
Attachment that you add to the email.

That way, once the file is sent, and you call dispose on the
Message, the file will be deleted automatically.

The reason this happens is for one of two reasons. First could be
that you are using the constructor for Attachment which is taking the
filename as a string. When this happens, it opens the file for read
access, and only allows read operations to be performed on the file
while the stream is open. The stream is still open when the event is
fired.

The second could be that you are not setting the share mode on
your FileStream to the appropriate value when creating it. You could
fix it on this level, and then manually delete the file, but I think
letting the OS do it is better.

Hope this helps.

Dec 5 '05 #5
Nick,

Keep in mind that it may take time from the moment the API for closing a
file is called at the moment a file is actually closed. This is because file
closing API just put a request in a queue. When the request is going to be
served by the kernel... well no one can say for sure. Experience for me says
that it is almost impossible to have
[close file]
[delete file]

adjacent in your code. At least you need to separate them with some idle or
sleep time
--

Stoitcho Goutsev (100) [C# MVP]

"Nick Z." <pa*****@gmail. com> wrote in message
news:Z2******** ********@fe09.l ga...
When using SmtpClient and sending a message with an attached file using
SendAsync(...), if I add File.Delete(pat hToAttachedFile ) in the
SendCompleted event handler I get a file in use exception. Is there a way
to overcome this problem?

Thanks,
Nick Z.

Dec 6 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2283
by: Michele Belloli | last post by:
Hi folks... I have a problem uploading a file to a web server. I created an html page with a multipart/form-data form and used cgi.parse_multipart function to obtain form data (and obviuosly uploaded file). I noticed that if I upload an excutable file, I obtain a file where 0x0a 0x0a, are converted in 0x0a and 0x2e. Obviously this file is corrupted.
1
2207
by: Jonathan | last post by:
Hi everyone, I have a problem with the file uploading in Asp.Net and I have read a lot on forums on this but never found an answer. Here is the problem: I know Asp.Net maximum Length for uploading a file to the server is 4Mo but I changed that maximum to about 10Mo in my web.config file : <httpRuntime executionTimeout="45" maxRequestLength="10000" useFullyQualifiedRedirectUrl="true" />
3
4319
by: prodirect | last post by:
Hi all, I hope someone can help me. I've recently created a database and wanted to put it up on an ftp sight so that multiple people could access the same tables at the same time from different geographical locations. I have been completely unsucessful in acheiving this goal so far however. Things I have tried: Create a shortcut to ftp sight via browser then tried to map local drive to
5
2735
by: IkBenHet | last post by:
Hello, I use this script to upload image files to a folder on a IIS6 server: ******************* START UPLOAD.ASPX FILE ********************** <%@ Page Language="VB" Debug="true" %>
8
1733
by: supasnail | last post by:
I have a happily working set of asp pages which read from the database via include file "./_private/include/database.mdb". However, when I try to gain access to this database on pages one folder removed using "../_private/include/database.mdb", the pages won't display. This whole system works fine on my home test server (iis.5.0), but 'breaks' when uploaded to the public server. I know the path to database is correct because the upper...
15
4144
by: =?ISO-8859-1?Q?J=F8rn?= Dahl-Stamnes | last post by:
Hello folks, I need some help/advice FAST. I have problems with addslashes on my web-servers. After uploading a file, I read the uploaded file, use addslashes on the read data and then insert it into a blob field in a MySQL database. The problem is that this works fine on my internal test web-server (running under RedHat 7.3). But on my production web-server (running Fedora Core 4)
4
10550
by: Tony B | last post by:
I've moved an existing site (which I didn't write) from a apache/php/mysql host under windows to a linux apache/php/mysql host. I've sorted out most problems except one. There is an upload function on the site, which uploads files via POST to temp folder and then moves it into a folder on the host using php function move_uploaded_file. Under windows this works fine but on the linux host the uploaded file is created with 600 permissions so...
3
4006
by: kujtim | last post by:
i got html code file name html <html> <head> <title></title> </head> <body>
11
2010
by: agarwalsrushti | last post by:
Hi, Ive created a registration page in which at the last it asks the user to upload the resume file. When the user clicks on the upload button to upload the file it automativcally submits all the data entered in the form. I want that when the user uploads the file after clicking the upload button it should display the message of successful upload on the same page itself. Then when the user clicks on the submit button the data should be entered...
0
9487
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10069
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9904
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9735
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7285
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6556
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5168
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5324
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3828
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.