473,397 Members | 1,949 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

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 1920
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
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
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
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
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
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
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
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
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
by: ziycon | last post by:
I'm getting this error when i try to attach a DB:
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
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...
0
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,...
0
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
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,...

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.