473,395 Members | 1,938 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,395 software developers and data experts.

Delete File

In an asp.net app, I'm trying to do some file upload / deleting for an image
gallery.

Deleting an image works great, except right after I have uploaded the file -
I cannot seem to get file access.

What do I need to do to solve this problem?

public bool DeleteImage(string fileName){

if (File.Exists(fileName))

{

try

{

if (this.WaitForExculsiveAccess(fileName))

{

System.IO.File.Delete(fileName);

return true;

}

else

{

return false;

}

}

catch (Exception e3)

{

return false;

}

}

else

{

return false;

}

}

private bool WaitForExculsiveAccess(string FullPath)

{

int ccount=0;

while (true)

{

if (ccount<5)

{

try

{

FileStream fs = new FileStream(FullPath, FileMode.Append, FileAccess.Write,
FileShare.None);

fs.Close();

return true;

}

catch

{

ccount++;

Thread.Sleep(1000);

}

}

else

{

return false;

}

}

}
Nov 18 '05 #1
3 1756
Is it possible that the file hasn't finished being uploaded before you
try to delete it? It depends on the details of how you're uploading the
file. I've had that problem with file uploads in the past when I have
used file system notification to detect the presence of a new file.

Bruce Johnson
http://www.ObjectSharp.com/Bruce

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 18 '05 #2
I've tried 2 different methods of uploading the file and it doesn't seem to
make a difference. what is file system notificaion?

"BruceJohnson" <Br***@ObjectSharp.removethis.com> wrote in message
news:up**************@TK2MSFTNGP12.phx.gbl...
Is it possible that the file hasn't finished being uploaded before you
try to delete it? It depends on the details of how you're uploading the
file. I've had that problem with file uploads in the past when I have
used file system notification to detect the presence of a new file.

Bruce Johnson
http://www.ObjectSharp.com/Bruce

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 18 '05 #3
File system notification is just a way of having an event raised when
something (like the addition of a file) happens to the file system.
Probably doesn't have bearing on what you're doing.

If I were in your situation, I'd be looking at the upload code to see
exactly what is happened to the file (such as, when is it being closed)
especially in relation to when you are trying to delete the file. Would
it be possible to post the upload code or provide some idea of what it's
doing and then the delete is called?

Bruce Johnson
http://www.ObjectSharp.com/Bruce

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 18 '05 #4

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

Similar topics

5
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...
0
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....
5
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...
23
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...
12
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...
9
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)...
2
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...
5
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...
3
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...
7
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) ...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...

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.