tags:

views:

1309

answers:

7

Seems like it should a really easy topic, all the examples everywhere are just a couple of lines however no decent explainations, and thus I keep running into the same error without a solution.

In short this part of the applicaion runs like so

  1. Pulls images from db
  2. Creates actual images files in a temp folder
  3. creates pdf with images inside of it
  4. now delete images that were created.

Everything works up until the delete. I keep getting the error

InnerException:
System.ArgumentException: URI formats are not supported. at System.IO.Path.NormalizePathFast(String path, Boolean fullCheck)...

I have tried a couple different ways to accomplish the delete the latest being:

foreach (string item in TempFilesList)
    {
        path = System.Web.HttpContext.Current.Application["baseWebDomainUrl"] + "/temp/" + item;
        fileDel = new FileInfo(path);
        fileDel.Delete();
    }

and the try before that one was:

foreach (string item in TempFilesList)
    {
        File.Delete(System.Web.HttpContext.Current.Application["baseWebDomainUrl"] + "/temp/" + item);
    }

TempFilesList is an array list containing the paths to the images to delete.

+8  A: 

You should try calling Server.MapPath(path) to get the "real" path to the file. Pass that to File.Delete, and it should work (assuming file permissions etc. are correct)

So for example:

foreach (string item in TempFilesList)
{
    path = System.Web.HttpContext.Current.Application["baseWebDomainUrl"] + "/temp/" + item;
    path = Server.MapPath(path);
    fileDel = new FileInfo(path);
    fileDel.Delete();
}
Ch00k
+1  A: 

Maybe using Server.MapPath(path) would help?

foreach(string item in TempFilesList)
{
  string path = String.Format("{0}/temp/{1}", HttpContext.Current.Application["baseWebDomainUrl"], item);
  File.Delete(Server.MapPath(path));
}

And it's better to use static File.Delete() instead of creating new FileInfo(path) just for deletion

abatishchev
+6  A: 

You need the actual file path of the file that you've created, not the URL of the path that you've created. Your code creates a path that look something like "http://www.mywebsite.com/location/temp/filename.jpg".

You need something that looks like "C:\MyWorkingFolder\filename.jpg".

I would recommend against using Server.MapPath, however. Since you are creating the files yourself in your own code, you control the location of where the file is being created. Use that, instead. Store it in as an AppSettings key in your web.config.

For example:

string basePath = ConfigurationManager.AppSettings["PdfGenerationWorkingFolder"];
foreach(string item in TempFilesList)
{
  File.Delete(basePath + item);
}
Randolpho
Why avoid using Server.MapPath()?
Nick
Well, there's absolutely no reason why the file needs to be addressable by the server, and Server.MapPath only works for web addressable files. In truth, any sysadmin worth his salt will deny the user under which a web server executes write access to anything that is web addressable.
Randolpho
I agree, most files (and certainly temporary files) should not be written under the web root, thought there might be another problem with MapPath..
Nick
+1  A: 

what pdf-creater-tool are you using? have you checked if it support a stream as input parameter? then you dont have to write the image temporary as a file.. and the hole problem is gone... -btw, I voted for Randolphos answer :p

ThorHalvor
A: 

If you saved the images, you must of used a file path to do that. Can you not just use that same file path to delete them? This would mean that you get away from any concatenating of file paths twice. (which as a side note: you should probably use Path.Combine for)

Andrew Cox
+1  A: 

For a Windows system you would use \ rather than / to separate folders in the path. For the code to work on any platform, use the Path.Combine method to put the path together:

path = Path.Combine(Path.Combine(System.Web.HttpContext.Current.Application["baseWebDomainUrl"], "temp"), item);
Guffa
A: 

I would only add this just in case you wanted it to hit the Recycling Bin rather than blow it out of existence:

FileSystem.DeleteFile(file.FullName, UIOption.OnlyErrorDialogs, RecycleOption.SendToRecycleBin, Microsoft.VisualBasic.FileIO.UICancelOption.DoNothing);

The only thing that sucks is that in your C# you have to reference a VisualBasic namespace

using Microsoft.VisualBasic.FileIO;

In your case you might not even need the bin since it's in the Database but I thought I'd throw it out there.

hunter