一些文件系统接口的使用

王朝other·作者佚名  2006-02-01
宽屏版  字体: |||超大  

一些文件系统接口的使用有句话好象是这么说来着,如果做了3年WINDOWS程序员,连COM都不会,那就是程序员的悲哀。

我发现WINDOWS下很多东西都是COM,并且会给你带来异想不到的作用。

比如下面的一些文件系统接口

此程序取得d:\film 文件夹的大小,并打印出此目录下所有文件名(不包括子目录),这些接口还有很多功能。

#include "stdafx.h"

#import "scrrun.dll" raw_interfaces_only

#include <iostream>

using namespace std;

using namespace Scripting;

int main()

{

::CoInitialize(NULL);

try

{

CComPtr<IFileSystem3> spFileSystem;

HRESULT hr=spFileSystem.CoCreateInstance(__uuidof(FileSystemObject));

CComPtr<IFolder> spFolder=NULL;

hr=spFileSystem->GetFolder(CComBSTR(L"d:\\film"), (IFolder **)&spFolder.p);

VARIANT vSize;

//取得文件夹大小

hr=spFolder->get_Size(&vSize);

cout << "Folder size is:" << vSize.dblVal << "bytes" << endl;

CComPtr<IFileCollection> spFileCollection;

hr=spFolder->get_Files((IFileCollection **)(&spFileCollection.p));

long lFileCount=0;

//取得文件夹下面的文件数目(不包括子文件夹)

hr=spFileCollection->get_Count(&lFileCount);

cout << "There are " << lFileCount << " files in the folder" << endl;

IUnknown *pIunknown=NULL;

hr=spFileCollection->get__NewEnum(&pIunknown);

if (SUCCEEDED(hr))

{

CComPtr<IEnumVARIANT> spEnum;

hr = pIunknown->QueryInterface(IID_IEnumVARIANT,(void**)&spEnum);

if(spEnum)

{

spEnum->Reset();

ULONG fget = 1;

while (SUCCEEDED(hr) && fget>0)

{

IFilePtr file;

_variant_t varfile;

hr = spEnum->Next(1, &varfile, &fget);

//取得文件名

if (SUCCEEDED(hr) && fget>0)

{

file = varfile.pdispVal;

BSTR bspath;

file->get_Path(&bspath);

cout << (LPCTSTR)_bstr_t(bspath) << endl;

}

}

}

}

pIunknown->Release();

spFileCollection.Release();

spFileSystem.Release();

spFolder.Release();

}

catch (_com_error &error)

{

_bstr_t bstrSource(error.Source());

_bstr_t bstrDescription(error.Description());

cout << "COM error occurred,source:" << (LPCTSTR)bstrSource << endl;

cout<< "Description:" << (LPCTSTR)bstrDescription << endl;

}

::CoUninitialize();

return 0;

}

今天突然间写代码写得睡不着了,晕 J

温柔的毒药 2005-10-17 23:50

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
© 2005- 王朝网络 版权所有