简体   繁体   中英

Rename file in use by my application

I've done a search and found a few similar threads to my own, though their fixes haven't really helped me.

I'm making an image management software and have the images opened in a file-explorer type of listview, then previewed in a picturebox on click. I wanted to have an option to rename the image file (eg if it's something like 234234234.jpg, rename it to something relevant like "trip with parents.jpg"). The problem is that the file is always in use by my application. I've tried disposing all the image objects as well as the controls using them, but nothing is working properly. I always get that IOException that the file is in use by another process (my program).

I'm using File.Move and have tried My.Computer.FileSystem.RenameFile .

My current code:

If BasePathWithItem = "" Then Exit Sub

File.Move(BasePathWithItem, BasePath & txtPicTitle.Text & txtExtension.Text)
PopulateImgList()

Any help is appreciated. I'm good with both VB and C# so help in either language is acceptable.

Thanks very much

Bitmap objects keep open the stream from which they are created. (See the "Remarks" section here .) One solution to your problem is to read the entire image file into a MemoryStream and then create the Bitmap from the MemoryStream . Close the original FileStream , and you'll be able to move the file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM