Delete a file

N

Neven Klofuar

hi,

I have a problem when trying to delete a file.

I have to extract some information from a file, and then I have to delete
it. When I try to delete it after I read it, I get a "Access denied" error.

help pls, Neven

********************************
StreamReader sr = new StreamReader(sFolderArhiva + sShortFileName);
while ((sLine = sr.ReadLine()) != null) {
try {
if (sLine.Substring(0, "ApplicationItem=".Length) ==
"ApplicationItem=") {
docType =
DohvatiTipDokumenta_TipDokumenta(sLine.Substring("ApplicationItem=".Length));
}
} catch (){}

try {
if (sLine.Substring(0, "ApplicationTag=".Length) ==
"ApplicationTag=") {
ID = sLine.Substring("ApplicationTag=".Length);
}
} catch {}
}

sr.Close();
sr.Dispose();
 
M

Morten Wennevik

Hi Neven Klofuar,

Are you sure you aren't holding the file in some other part of your code,
or another program like a text editor. Can you delete the file manually?

A tip when using disposable/closable objects use a using statement

Instead of

StreamReader sr = new StreamReader(sFolderArhiva + sShortFileName);
// additional code here
sr.Close();
sr.Dispose();

Use

using(StreamReader sr = new StreamReader(sFolderArhiva + sShortFileName))
{
// additional code here
}

Makes the scope of the streamreader easier to see.
 
G

Guest

Neven,

Check if ASPNET account (unless you use impersonation) has write access?

Regards,
Augustin
 
N

Neven Klofutar

Thanks for the answers, I noticed that file I'm trying to delete has a
read-only property, so I have to find a way to remove that property.

Neven
 
M

Morten Wennevik

Removing the ReadOnly attribute can be done like this

FileInfo fi = new FileInfo(sFolderArhiva + sShortFileName);

if((fi.Attributes & FileAttributes.ReadOnly) > 0)
fi.Attributes -= FileAttributes.ReadOnly;

fi.Delete();
 
N

Neven Klofutar

yup, found that one ...

Thank you all for the help !!!

Neven



Removing the ReadOnly attribute can be done like this

FileInfo fi = new FileInfo(sFolderArhiva + sShortFileName);

if((fi.Attributes & FileAttributes.ReadOnly) > 0)
fi.Attributes -= FileAttributes.ReadOnly;

fi.Delete();
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top