By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,665 Members | 1,367 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,665 IT Pros & Developers. It's quick & easy.

Unable to attach file

P: n/a
I have a windows service that monitors a directory for newly created files.
When one is found, an e-mail is generated and the new file is added as an
attachment. In order to avoid trying to attach the file if it is still in the
process of being written to, I'm using a routine (that I found somewhere on
this site, but can't seem to locate the thread anymore...) that checks to see
if that file can be opened using a FileStream. If it can, it is attached and
e-mailed. If it can't (i.e. it's still being written to) I wait for a second
and try again.

This code works like a charm on my local system, but when I move it to the
QA box I continuously get a "File not found" error. I've double checked that
the file exists and that everything is spelled correctly. I have a hunch that
it has something to do with the permissions that the service has in terms of
reading/writing to files. Here is the code I'm using:

int timeout = 5000;
FileStream fs;
FileInfo fi = new FileInfo("D:\\reports\\SchedulerOutput\\TestReport .rtf");
while(timeout > 0)
{
try
{
fs = fi.Open(FileMode.Open, FileAccess.ReadWrite);
fs.Close();
break;
}
catch
{
System.Threading.Thread.Sleep(1000);
timeout -= 1000;
}
}

if(timeout <= 0)
{
eventLog.WriteEntry("Timeout...");
}
else
{
MailMessage m = new MailMessage();
m.From = <from e-mail>;
m.To = <to e-mail>;
m.Subject = "Test";
m.Attachments.Add(new MailAttachment
"D:\\reports\\SchedulerOutput\\TestReport.rtf" ));
System.Web.Mail.SmtpMail.Send(m);
}

Can anybody shed any light on this? Thanks!

Brian
Jul 21 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Update:

Digging a little deeper, it seems that the actual point of failure is in the
creation of the FileStream object via the FileInfo.Open method. The actual
exception being thrown is an UnauthorizedAccessException: "Access to the file
'D:\reports\SchedulerOutput\TestReport.rtf' is denied".

I have increased the assembly trust for the .exe, I have given the Window's
account that the service runs under full access for that directory...still no
luck.

Thoughts?

"bmcelhany" wrote:
I have a windows service that monitors a directory for newly created files.
When one is found, an e-mail is generated and the new file is added as an
attachment. In order to avoid trying to attach the file if it is still in the
process of being written to, I'm using a routine (that I found somewhere on
this site, but can't seem to locate the thread anymore...) that checks to see
if that file can be opened using a FileStream. If it can, it is attached and
e-mailed. If it can't (i.e. it's still being written to) I wait for a second
and try again.

This code works like a charm on my local system, but when I move it to the
QA box I continuously get a "File not found" error. I've double checked that
the file exists and that everything is spelled correctly. I have a hunch that
it has something to do with the permissions that the service has in terms of
reading/writing to files. Here is the code I'm using:

int timeout = 5000;
FileStream fs;
FileInfo fi = new FileInfo("D:\\reports\\SchedulerOutput\\TestReport .rtf");
while(timeout > 0)
{
try
{
fs = fi.Open(FileMode.Open, FileAccess.ReadWrite);
fs.Close();
break;
}
catch
{
System.Threading.Thread.Sleep(1000);
timeout -= 1000;
}
}

if(timeout <= 0)
{
eventLog.WriteEntry("Timeout...");
}
else
{
MailMessage m = new MailMessage();
m.From = <from e-mail>;
m.To = <to e-mail>;
m.Subject = "Test";
m.Attachments.Add(new MailAttachment
"D:\\reports\\SchedulerOutput\\TestReport.rtf" ));
System.Web.Mail.SmtpMail.Send(m);
}

Can anybody shed any light on this? Thanks!

Brian

Jul 21 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.