简体   繁体   English

重命名我的应用程序正在使用的文件

[英]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. 我正在制作一个图像管理软件,并以文件浏览器类型的listview打开图像,然后单击单击在图片框中预览图像。 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"). 我想有一个重命名图像文件的选项(例如,如果它类似于234234234.jpg,则将其重命名为与“ 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). 我总是得到那个IOException,该文件正在被另一个进程(我的程序)使用。

I'm using File.Move and have tried My.Computer.FileSystem.RenameFile . 我正在使用File.Move ,并尝试了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. 我对VB和C#都很满意,因此可以接受两种语言的帮助。

Thanks very much 非常感谢

Bitmap objects keep open the stream from which they are created. Bitmap对象将打开从其创建对象的流。 (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 . (请参阅此处的“备注”部分。)解决问题的一种方法是将整个图像文件读入MemoryStream ,然后从MemoryStream创建Bitmap Close the original FileStream , and you'll be able to move the file. 关闭原始FileStream ,您将能够移动文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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