Akshay Pisal
Akshay Pisal

Reputation: 41

file is saved with zero bytes(0 bytes)

0 bytes file created when i am trying to replace the file in code behind of C#.

  string FileExactLocation = s + str;
  if (File.Exists(FileExactLocation))
  {
      File.Replace(FileExactLocation, FileExactLocation,"werwe");
  }   
  else
      FileUpload1.SaveAs(FileExactLocation);

in above code, i am trying to delete a file located on server,it gets deleted but the file which i am trying to save (replace) contains 0 bytes...its empty...

Please give me the solution to this problem....

Upvotes: 2

Views: 2909

Answers (3)

Akshay Pisal
Akshay Pisal

Reputation: 41

the reason is the fileUploadcontrol value is cleared after postback...and hence u r creating .zip file with no data.. in that case u can upload file using byte streaming....

Upvotes: 0

Hans Olsson
Hans Olsson

Reputation: 55009

I think you're confused about what Replace does. If all you want to do is to replace the original file with a new one, just delete the old one, otherwise, if you do want to keep a backup of the original file, rename it before the save.

So if my understanding is correct, I think you're looking for either:

  string FileExactLocation = s + str;
  if (File.Exists(FileExactLocation))
  {
      File.Delete(FileExactLocation);
  }   
  FileUpload1.SaveAs(FileExactLocation);

Or:

  string FileExactLocation = s + str;
  if (File.Exists(FileExactLocation))
  {
      // Rename the file adding werwe to the filename
      File.Move(FileExactLocation, Path.Combine(FileExactLocation, "werwe"));
  }   
  FileUpload1.SaveAs(FileExactLocation);

Upvotes: 1

RaM
RaM

Reputation: 1142

If I am not wrong there is a memory leak somewhere in your code, do you want to try using the "using()" statement and also make sure you dispose all objects which is associated with the file

Upvotes: 1

Related Questions