473,505 Members | 14,658 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Unable to attach file

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
1 1931
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
3933
by: Jimmy | last post by:
Hi, I was trying to debug two different ASP.NET projects at the same time. But I was only able to debug one of them and I got the following error for the other one: "Unable to start debugging...
2
4792
by: TM | last post by:
When I run an ASP.Net application I am getting the following error: "Error while trying to run project: Unable to start debugging on the web server. The project is not configured to be debugged."...
1
402
by: bmcelhany | last post by:
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...
1
2132
by: SenthilVel | last post by:
Hi I have a question when debugging dotnet code when attaching vb6.exe code to this process. i get an error like : unable to attach the process. there is no managed code running in the...
6
6428
by: eric.goforth | last post by:
Hello, I'm calling a VB.NET 2003 dll that's registered in COM+. I've added a reference to it in a VB6 DLL that I'm calling from a Classic ASP page. Previously I was able to attach to my .NET...
2
2041
by: prajeeshv | last post by:
Hi Everyone, Have a nice day... I've a problem in SQL Server... I've detached my database from server. But I didn't remember the location of its .mdf (database) file. Now I'm...
3
24222
by: weird0 | last post by:
This is the exception that I get when i create a webserivce obj and call. The error comes on the webmethod call, when it opens a connection to the db. How do I fix it? What is the solution to...
0
3122
by: DR | last post by:
Unable to start TSQL Debugging. Could not attach to SQL Server Process on 'srvname'. The RPC server is unavailable. I get this error when I try to run a SQL Server Project with a CLR stored...
1
5590
by: ziycon | last post by:
I'm getting this error when i try to attach a DB:
0
7216
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,...
0
7098
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
7018
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7471
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...
0
5613
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4699
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...
0
3187
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...
0
3176
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
407
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.