473,785 Members | 2,987 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Delete a file

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(sF olderArhiva + sShortFileName) ;
while ((sLine = sr.ReadLine()) != null) {
try {
if (sLine.Substrin g(0, "ApplicationIte m=".Length) ==
"ApplicationIte m=") {
docType =
DohvatiTipDokum enta_TipDokumen ta(sLine.Substr ing("Applicatio nItem=".Length) );
}
} catch (){}

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

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

------------

try {
File.Delete(sFo lderArhiva + sShortFileName) ;
} catch {}


Sep 12 '06 #1
5 1471
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(sF olderArhiva + sShortFileName) ;
// additional code here
sr.Close();
sr.Dispose();

Use

using(StreamRea der sr = new StreamReader(sF olderArhiva + sShortFileName) )
{
// additional code here
}

Makes the scope of the streamreader easier to see.

--
Happy Coding!
Morten Wennevik [C# MVP]
Sep 12 '06 #2
Neven,

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

Regards,
Augustin
"Neven Klofuar" wrote:
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(sF olderArhiva + sShortFileName) ;
while ((sLine = sr.ReadLine()) != null) {
try {
if (sLine.Substrin g(0, "ApplicationIte m=".Length) ==
"ApplicationIte m=") {
docType =
DohvatiTipDokum enta_TipDokumen ta(sLine.Substr ing("Applicatio nItem=".Length) );
}
} catch (){}

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

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

------------

try {
File.Delete(sFo lderArhiva + sShortFileName) ;
} catch {}


Sep 12 '06 #3
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
"Neven Klofuar" <neven.klofutar @**r.e.m.o..v.. .e**vip.hrwrote in message
news:e4******** ********@TK2MSF TNGP02.phx.gbl. ..
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(sF olderArhiva + sShortFileName) ;
while ((sLine = sr.ReadLine()) != null) {
try {
if (sLine.Substrin g(0, "ApplicationIte m=".Length) ==
"ApplicationIte m=") {
docType =
DohvatiTipDokum enta_TipDokumen ta(sLine.Substr ing("Applicatio nItem=".Length) );
}
} catch (){}

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

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

------------

try {
File.Delete(sFo lderArhiva + sShortFileName) ;
} catch {}


Sep 12 '06 #4
Removing the ReadOnly attribute can be done like this

FileInfo fi = new FileInfo(sFolde rArhiva + sShortFileName) ;

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

fi.Delete();

--
Happy Coding!
Morten Wennevik [C# MVP]
Sep 12 '06 #5
yup, found that one ...

Thank you all for the help !!!

Neven

"Morten Wennevik" <Mo************ @hotmail.comwro te in message
news:op.tfq8fqp 9klbvpo@tr024.. .
Removing the ReadOnly attribute can be done like this

FileInfo fi = new FileInfo(sFolde rArhiva + sShortFileName) ;

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

fi.Delete();

--
Happy Coding!
Morten Wennevik [C# MVP]
Sep 12 '06 #6

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

Similar topics

5
14154
by: Jobs | last post by:
Hello All, I want to delete all files in a directory. I am making a backup copy of all files in the directories say c:\abc by reading and writing to a file. After making a backup copy I want to delete these files. I am using following code: // get list of all files in the directories
0
2104
by: SeanR | last post by:
I have a function to copare two files. It will first copy the original file form a different server to a local temp path and then compare that version to a version that has been restored form tape. Once the compare is complete the file that was copied to a temp location needs to be deleted. I am using the method file.copy(sourcePath, tempPath, true) to copy the file and then file.delete(tempPath) to delete the file. On some of the files...
5
5247
by: | last post by:
Hi all, I've been using C++ for quite a while now and I've come to the point where I need to overload new and delete inorder to track memory and probably some profiling stuff too. I know that discussions of new and delete is a pretty damn involved process but I'll try to stick to the main information I'm looking for currently. I've searched around for about the last too weeks and have read up on new and overloading it to some extent but...
23
8949
by: da Vinci | last post by:
Greetings, Onwards with the school studying. Working on a program and need to delete a file from a known location on the hard drive but cannot get anything I do to work. I have tried to use the remove function that is included with <cstdio> but cannot get it to work properly. My reference book has the following....
12
8019
by: Lucas Tam | last post by:
I have a very simple loop: If (Directory.Exists(tempDirectory)) Then Try Dim Files() As String = Directory.GetFiles(tempDirectory) 'Clear out directory For Each Filename As String In Files File.Delete(Filename) Next Catch ex As Exception
9
8336
by: Paul | last post by:
I'm trying to make get my app to delete all the files in a specified folder and all the files within the folders of the specified folder. e.g. Folder 1 contains three files (File1, File2, File3) and two folders (Subfolder 1, Subfolder 2). .......I need to delete File1, File2, File3. Subfolder 1 contains FileA. .......Need to delete FileA. Subfolder 2 contains FileB, FileC, FileD.
2
4750
by: createdbyx | last post by:
I am trying to make a file sync utillity to sync files between my laptop and my desktop pc. On my desktop machine (xp pro sp2) I have shared my "Visual Studio Projects" folder using windows simple file sharing. And have specified the "Allow network users to change my files." option as well. Then on my laptop (xp home sp2) I have mapped a network drive using windows explorer. So on my laptop I have a Y: drive that points to the "Visual...
5
39051
by: wo20051223 | last post by:
Deleting some files with C# fails with "Access to the path 'X' is denied". I have files copied from a CD that I burned (and not locked by a process) and a text file that I created in Windows Explorer. I can delete all of them through Windows Explorer. I can programmatically delete the text file but not the others. Permissions: - All files have the same ACL.
3
3282
by: Arpan | last post by:
A Form has a FileUpload, 2 Buttons & a TextBox web server controls. Using the FileUpload control, I want to give users the provision to move & delete files that DO NOT exist in C:\Inetpub\wwwroot (i.e. the root directory). This is the code: <script runat="server"> Sub MoveFile(ByVal obj As Object, ByVal ea As EventArgs) File.Move(fudFileSource.FileName, txtFileDest.Text) 'File.Move("F:\4.jpg", "C:\4.jpg") End Sub
7
4223
by: Anil Gupte | last post by:
Private Sub mnu2Exit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnu2Exit.Click Dim fDir As String = Path.GetDirectoryName(L3Global.VideoFileName) File.Delete(L3Global.VideoFileName) ' The following is not working - reports directory not empty exception ' Directory.Delete(fDir)
0
9480
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10327
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
9950
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
7499
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
6740
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
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4053
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
2
3647
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2879
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.