如何检索文件信息.

编程入门 行业动态 更新时间:2024-10-12 05:53:18
本文介绍了如何检索文件信息.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

所有人的祝福, 有人知道如何检索文件的文件大小吗?或尽可能多的文件的所有信息? 在获取文件的大小时,我正在使用FileStream.但并非所有文件都可以访问.有些问题是由于它正在被另一个进程使用,或者访问被拒绝.如何缓解这些问题? 希望您能给我实际的代码. 非常感谢!! :)

解决方案

这是我使用的代码. file ="C:\ mytext.txt"; 使用(FileStream Fsize = File.OpenRead(file)){ FileInfo.Size =(double)Fsize.Length; } :格式化尺寸 私有字符串FormatSize(双倍大小) { string []大小= {"B","kB","MB","GB","TB","PB","EB","ZB","YB"}}; double formattedSize = size; Int32 sizeIndex = 0; while(formattedSize> = 1024&&size;< sizes.Length) { formattedSize/= 1024; sizeIndex ++; } 返回Math.Round(formattedSize,2).ToString()+" + size [sizeIndex]; } 我想要的是即使文件打开也可以检索文件的文件大小 或当前正在由其他进程使用.就像Windows Os所具有的一样,当右键单击文件时,您将获得包括文件大小在内的信息. 谢谢!

您好, 您可以通过使用FileInfo类来完成此操作,而不是将文件加载到FileStream中. 即使文件正在使用中,FileInfo类也将起作用,并且不会引发异常.如果使用File.OpenRead(file),将无法使用. 这是示例代码:

字符串 file = @" c:\ pagefile.sys"; FileInfo f = 新 FileInfo(file); 长 size = f.Length; double formattedSize = size; 字符串 []大小= {" B" ," kB" ," MB"," GB"," TB" ," PB" ," EB" ," ZB"," YB"}; Int32 sizeIndex = 0 ; 同时(formattedSize > = 1024 & ;& sizeIndex < size.Length) { formattedSize/= 1024 ; sizeIndex ++; } MessageBox.Show(Math.Round(formattedSize, 2 ).ToString()+ " " + size [sizeIndex]);

Valery.

除了Abhinav的解决方案外,您还可以使用FileInfo class''Lenght Property来获取文件的大小... 这是MSDN 链接 [ ^ ]

Good day to all, Anyone know how to retrieve the file size of a file? or as much as possible all information of the file? In getting the size of the file, I am using FileStream. But not all the files can be accessed. some problems are because of it is being used by another process, or access is denied. How to ease these problems? Hopefully you can give me the actual code. Thanks a lot!! :)

解决方案

here are the codes i used. file = "C:\mytext.txt"; using (FileStream Fsize = File.OpenRead(file)){ FileInfo.Size = (double)Fsize.Length; } : to format the size private string FormatSize(double size) { string[] sizes = {"B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"}; double formattedSize = size; Int32 sizeIndex = 0; while (formattedSize >= 1024 && sizeIndex < sizes.Length) { formattedSize /= 1024; sizeIndex++; } return Math.Round(formattedSize, 2).ToString() + " " + sizes[sizeIndex]; } What i want to have is that retrieve the filesize of the file, even if it is open or currently using by other process. Just like what windows Os has, when you right click on the file, you get its information including the filesize. Thanks!

Hello, You could do it by using the file FileInfo class rather than loading your file in a FileStream. The FileInfo class will work even if the file is in use and will not throw an exception. If you use File.OpenRead(file)it will not work. Here is a sample code:

string file = @"c:\pagefile.sys"; FileInfo f = new FileInfo(file); long size = f.Length; double formattedSize = size; string[] sizes = {"B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"}; Int32 sizeIndex = 0; while (formattedSize >= 1024 && sizeIndex < sizes.Length) { formattedSize /= 1024; sizeIndex++; } MessageBox.Show(Math.Round(formattedSize, 2).ToString() + " " + sizes[sizeIndex]);

Valery.

In addition to Abhinav''s solution you can use FileInfo class'' Lenght Property to get size of a file... Here is MSDN link[^]

更多推荐

如何检索文件信息.

本文发布于:2023-11-06 08:27:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1563189.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件   信息

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!