是否可以在不使用C#的情况下获取文件的大小System.IO.FileInfo?
我知道你可以分别使用Path.GetFileName(yourFilePath)和得到其他东西,比如Name和Extension Path.GetExtension(yourFilePath),但显然不是文件大小?有没有其他方法我可以获得文件大小而不使用System.IO.FileInfo?
唯一的原因是,如果我是正确的,FileInfo会抓取比我真正需要的更多的信息,因此如果我需要的唯一东西是文件的大小,那么收集所有这些FileInfo需要更长的时间.有更快的方法吗?
嗯,标题说明了一切.将文件名传递给方法时,我应该使用FileInfo对象还是普通文件名(字符串)?为什么我更喜欢一个到另一个?
我的一些同事喜欢写这样的方法:
它比以下更好:
谢谢!
我在使用PHP 5.3.5和IIS 6在Windows Server 2003上运行的PHP脚本中调用finfo_open时遇到问题.调用始终返回致命错误:在...中调用未定义函数finfo_open()
通过一点阅读,我知道在Windows PHP安装中默认不包含fileinfo功能,但我尝试的任何东西都不能正常工作.评论#3中的说明:http://www.php.net/manual/en/fileinfo.installation.php没有帮助,这是我能找到的官方最正式的解释.有很多关于在网络上需要mime_magic dll的信息,但似乎从5.3开始就不再需要了.此外,我在http://pecl.php.net/package/Fileinfo上读到"从PHP 5.3.0开始,此扩展默认启用".这是怎么回事?
此问题在测试服务器上.在我的本地机器上我有xampp和PHP 5.3.1并且调用工作正常,所以我也尝试将php_fileinfo.dll从本地复制到php\ext进行测试,但这也没有任何区别(我知道版本是不同,但我读到5.3是重要的一点).
对此的任何建议将不胜感激!
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mime = finfo_file($finfo, $filepath);
Run Code Online (Sandbox Code Playgroud) 从属性的MSDN文档中FileInfo.Name,我看到属性的数据在第一次被调用时被缓存,并且随后将仅使用该Refresh方法进行更新.
我有以下问题,我在文档中找不到或不太清楚:
是否同时缓存了所有属性的数据?
该Refresh方法是在创建时调用的FileInfo,还是仅在第一次调用属性时调用?
如果我调用了一个属性,例如Name属性,并且它被调用Refresh,将DirectoryName首次调用另一个属性(例如属性)使其Refresh再次调用,或者它是否仅由在整个类中访问的第一个属性调用(见问题#1)?
我可以通过Refresh手动调用预先缓存所有属性吗?(假设它没有在构造对象时预先缓存)
Refresh手动调用是否会导致预先缓存的属性,例如CreationTime,还要刷新?
我正在访问SPLFileInfo对象中的许多文件.我看到了获取文件的路径,文件名甚至扩展名的方法.有没有办法获得没有扩展名的文件名?这是我一直在使用的代码,但我希望能得到更优雅的东西.有开箱即用的解决方案吗?
$file = new SplFileInfo("path/to/file.txt.zip");
echo 'basename: '.$file->getBasename();
echo PHP_EOL;
echo 'filename: '.$file->getFilename();
echo PHP_EOL;
echo 'extension: '.$file->getExtension();
echo PHP_EOL;
echo 'basename w/o extension: '.$file->getBasename('.'.$file->getExtension());
>>OUTPUT
>>basename: file.txt.zip
>>filename: file.txt.zip
>>extension: zip
>>basename w/o extension: file.txt
Run Code Online (Sandbox Code Playgroud) 我得到这个错误:LogicException:在尝试上传图像时,无法猜测mime类型,因为没有猜测器可用(你启用了php_fileinfo扩展吗?).我启用了php_fileinfo扩展,并重新启动了Wamp Web服务器,但我仍无法解决此问题.我错过了什么?谢谢
以下是我的代码:
型号:Product.php
class Product extends Eloquent {
protected $fillable = array('category_id', 'title', 'description', 'price', 'availability', 'image');
public static $rules = array(
'category_id'=>'required|integer',
'title'=>'required|min:2',
'description'=>'required|min:20',
'price'=>'required|numeric',
'availability'=>'integer',
'image'=>'required|image|mimes:jpeg,jpg,bmp,png,gif|max:3000',
);
public function category() {
return $this->belongsTo('Category');
}
Run Code Online (Sandbox Code Playgroud)
}
控制器:ProductsController.php
public function postCreate() {
$validator = Validator::make(Input::all(), Product::$rules);
if($validator->passes()) {
$product = new Product;
$product->category_id = Input::get('category_id');
$product->title = Input::get('title');
$product->description = Input::get('description');
$product->price = Input::get('price');
$image = Input::file('image');
$filename = date('Y-m-d-H:i:s')."-".$image->getClientOriginalName();
Image::make($image->getRealPath())->resize(468,249)->save('public/img/products/'.$filename);
$product->image = 'img/products/'.$filename;
$product->save();
return Redirect::to('admin/products/index')
->with('message', 'Product …Run Code Online (Sandbox Code Playgroud) 我想只从目录返回10个文件.这可能吗?
DirectoryInfo d = new DirectoryInfo(HttpContext.Current.Server.MapPath("~/xml"));
FileInfo[] files = d.GetFiles("*.xml");
Run Code Online (Sandbox Code Playgroud)
这种方式返回所有 XML文件,但我想得到前十个.
在我的应用程序(C#4.5 winforms app)中,我定期检查文件夹的内容并将找到的任何文件的详细信息存储到数据库中.在这个程序中,我创建了一个FileInfo使用实例new FileInfo(path),和我读的性能CreationTime,LastWriteTime,LastAccessTime,Length和Attributes.我从不在FileInfo实例上调用任何方法.
我想知道的是:如果在我创建FileInfo对象时,第三方应用程序当前正在写入文件(或者在被Windows复制到文件夹的过程中),是否存在损坏,锁定或运行时错误的风险访问它的属性?
我想在文件系统中添加和检索文件的标签。
正如您可以将 Stackoverflow 问题标记到相关主题一样,您可以在 Windows 文件系统中标记文件:
以下方法不允许我访问文件的标签,而只能访问其他文件属性。
string file = @"C:\Users\me\Desktop\MyFile.doc";
FileInfo oFileInfo = new FileInfo(file);
MessageBox.Show(oFileInfo.FullName);
Run Code Online (Sandbox Code Playgroud)
关于如何使用简单的 API 访问标签有什么想法吗System.IO?