473,804 Members | 2,249 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Realeasing file lock?

7 New Member
Hi,
I'm having a problem with releasing a file lock after a save. My code takes an image file uploaded via a form, and saves it to disk. That's fine, but I'm trying to add a new function which allows the user to choose to automatically create a thumbnail. At this stage, it throws a "System.OutOfMe moryException: Out of memory." Even if the files are small, I still get this, and I find that if I open up Windows Explorer and try to delete the large image, it's locked by the aspnet_wp process, so I'm guessing it's this lock that's causing the exception (which I know doesn't always mean it's actually out of memory!).
I have tried everything I can think of to release the lock (see the gubbins below!) but it's still not working. Any ideas?

Expand|Select|Wrap|Line Numbers
  1. public static Upload uploadFile(HtmlInputFile inputFile, string sSavePath, bool createThumb)
  2.         {
  3.             System.Web.HttpServerUtility Server=HttpContext.Current.Server;
  4.  
  5.             // If file field isn't empty
  6.             if (inputFile.PostedFile.ContentLength > 0)
  7.             {
  8.  
  9.                 // Check file size (mustn't be 0)
  10.                 HttpPostedFile myFile = inputFile.PostedFile;
  11.                 int iFileLen = myFile.ContentLength;
  12.  
  13.                 //<snip> do some checks to see if it's accepted file type etc </snip>
  14.  
  15.  
  16.                 // Read file into a data stream
  17.                 byte[] myData = new Byte[iFileLen];
  18.                 myFile.InputStream.Read(myData,0,iFileLen);
  19.  
  20.                 // Save the stream to disk
  21.                 System.IO.FileStream newFile = new System.IO.FileStream(savepath, System.IO.FileMode.Create);
  22.                 try
  23.                 {
  24.                     newFile.Write(myData,0, myData.Length);
  25.                 }
  26.                 catch{}
  27.  
  28.                 //anything i can think of to clear the file lock
  29.                 inputFile.Dispose();
  30.                 newFile.Flush();
  31.                 newFile.Close();
  32.                 newFile = null;
  33.                 myFile = null;
  34.                 GC.Collect();
  35.  
  36.  
  37.                 if(createThumb)
  38.                 {
  39.  
  40.                     string[] pth = savepath.Split('.');
  41.                     string thumbpath = "";
  42.                     for(int i=0;i<pth.Length-1;i++)
  43.                         thumbpath += pth[i];
  44.                     thumbpath += "_thumb";
  45.                     thumbpath += pth[pth.Length-1];
  46.  
  47.                     createThumbnail(savepath,thumbpath,150,150);
  48.  
  49.                 }
  50.             }
  51.         }
  52.  
  53.  
  54.  
  55.  
  56.         public static void createThumbnail(string FullImagePath, string thumbnailPath, int maxWidth, int maxHeight)
  57.         {
  58.             //read the full size image -- ***This is where the exception is thrown***
  59.             System.Drawing.Image fullSizeImg = System.Drawing.Image.FromFile(FullImagePath);
  60.  
  61.             // check the sizes to see if we need to resize
  62.             bool resize = false;
  63.             int thumbWidth = fullSizeImg.Width;
  64.             int thumbHeight = fullSizeImg.Height;
  65.  
  66.  
  67.             //check to see if the width is too large, set new sizes if required
  68.             if(thumbWidth > maxWidth)
  69.             {
  70.                 resize = true;
  71.                 thumbWidth = maxWidth;
  72.                 thumbHeight = maxWidth/fullSizeImg.Width * fullSizeImg.Height;
  73.             }
  74.  
  75.             // then we have to check to see if the height is within the constriants and try again
  76.             if(thumbHeight > maxHeight)
  77.             {
  78.                 resize = true;
  79.                 thumbHeight = maxHeight;
  80.                 thumbWidth = maxHeight/fullSizeImg.Height * fullSizeImg.Width;
  81.             }
  82.  
  83.             if(resize)
  84.             {
  85.                 System.Drawing.Image thumbNailImg = fullSizeImg.GetThumbnailImage(thumbWidth, thumbHeight, null, IntPtr.Zero);
  86.                 thumbNailImg.Save(thumbnailPath,ImageFormat.Jpeg);
  87.  
  88.                 //Clean up / Dispose...
  89.                 thumbNailImg.Dispose();
  90.             }
  91.         }
  92.  
Sep 27 '07 #1
1 1552
Plater
7,872 Recognized Expert Expert
Well I'm not sure I'm following what you're doing with htmlinputfile and htmlpostfile, but it looks like you're closing all your filehandles and everything correctly.
I guess I would say that when reading an image in, I recomend using the Bitmap class (even if it's a jpeg/gif/png/whatever don't worry about it) it might have a better ability to input the file?
Sep 27 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

6
7330
by: Pekka Niiranen | last post by:
Hi, I have used the following example from win32 extensions: -----SCRIPT STARTS---- import win32file import win32con import win32security import pywintypes
8
3813
by: Stephen Corey | last post by:
I'm writing an app that basically just appends text to a text file on a Win2K Server. They fill out a form, click a button, and 1 line is appended to the file. Multiple people will run this app at the same time, and all will write to the same file. If I do an immediate flush() on the file after writing the line, is there still a risk that 2 simultaneous writes will collide? If so, what's the best way to handle this type of file write? ...
6
2638
by: martin | last post by:
Hi, I have noticed that every aspx page that I created (and ascx file) has an assosiated resource file aspx.resx. However what I would like to do is have a single global resource file for the site. The resources in this global resource file will possibly change quite often, there I guess this rules out using web.config as every time web.config alters the application is recompiled, also I am not sure of the possible consequences of...
14
3840
by: Gary Nelson | last post by:
Anyone have any idea why this code does not work? FileOpen(1, "c:\JUNK\MYTEST.TXT", OpenMode.Binary, OpenAccess.ReadWrite, OpenShare.Shared) Dim X As Integer For X = 1 To 26 FilePut(1, Chr(X + 64)) Next Lock(1, 5, 10)
1
19970
by: ABCL | last post by:
Hi All, I am working on the situation where 2 different Process/Application(.net) tries to open file at the same time....Or one process is updating the file and another process tries to access it, it throws an exception. How to solave this problem? So second process can wait until first process completes its processing on the file. Thanks in advance
13
11160
by: George | last post by:
Hi, I am re-writing part of my application using C#. This application starts another process which execute a "legacy" program. This legacy program writes to a log file and before it ends, it writes a specific string to the log file. My original program (MKS Toolkit shell program) which keeps running "grep" checking the "exit string" on the "log files". There are no file sharing problem.
0
865
by: Luckbox72 | last post by:
I am tring to open a compressed file with openfiledialog. Private Function openfile() As String Dim myFileName As String Dim myLoadDialog As New OpenFileDialog myLoadDialog.Filter = "zip|*.zip" If myLoadDialog.ShowDialog() = DialogResult.OK Then myFileName = myLoadDialog.FileName Else
5
4098
by: pgdown | last post by:
Hi, I have several processes accessing files from one folder, but only one process should ever access each file. Once one process has the file, no other process should be allowed to access it, even after the first process is finished with it, except in the case where the first process crashes. In pseudo code.. * Open file with exclusive lock * Process file - failure will throw exception, skipping 'Remove
2
2627
by: WingSiu | last post by:
I am writing a Logging util for my ASP.NET application. I am facing mulit process problem. I developed a class LogFactory, and have a method called Get_Logger to create a FileLogger, which will write text into a text file. // sample code FileLogger logger = LogFactory.Get_Logger(); logger.Log("Message here");
0
9594
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
10599
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
10346
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10347
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7635
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
6863
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
5531
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5673
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4308
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

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.