【开发小课堂】【1】【File.IO.Exist】

qiqiminmin UID.638527
2015-05-16 发表

本帖最后由 qiqiminmin 于 2015-5-17 20:08 编辑

这是我个人开发过程中的一些小经验,很多都是非常需要,但是windows开发团队没来得及做的。第一课就说文件是否存在吧, file exist

【背景】目前windows store 的api,以及windows 10的api,都没有开放 file exist,曾经在dev.windows上询问过,回答的是一个不在Stroage团队的微软工程师,大体意思是,目前api还没有开放,具体原因不知道为什么,但File.IO团队一直在努力(啊,当时很惊讶,为什么某个文件是否存在至今仍然没有加入到api),目前还不能做到直接得到一个File.Exist, 将来的目标也只是通过Get StoreFile时,返回一个null.

那我们看看目前官方指导方法

try
{
var file = await ApplicationData.Current.LocalFolder.GetFileAsync(fileName);
//no exception means file exists
}
catch (FileNotFoundException ex)
{
//find out through exception
}

也就是说,我每次想要知道某个文件是否存在,必须要 catch 一个 exception,
但实际过程中,你能在windows 8.1上抓住,也许你就不能在windows phone 8.1上抓住,特别是使用了sqlite的时候,总之一句话,不靠谱。

【设想】windows phone 8.1之后,可以支持 DllImport; 而desktop时,有时候我们使用这种方法获得文件信息,(我想只有曾经开发过win32的老人才知道这些东西吧,幸好自己弄过)
[mw_shl_code=cpp,true] BOOL WINAPI GetFileAttributesEx(
_In_ LPCTSTR lpFileName,
_In_ GET_FILEEX_INFO_LEVELS fInfoLevelId,
_Out_ LPVOID lpFileInformation
);
[/mw_shl_code]

【实现】首先有几个问题要注意一下 GetFileAttributesEx是kernel32.dll下的,首先要确定在windows phone 8.1下是否存在,凭感觉肯定不能直接用的,因为32意思是32位的意思,多直目前的x86架构的。
在网上一搜,还真有结果是 api-ms-win-core-file-l1-2-1.dll,好剩下的就是简单的东西,贴代码吧,免得大家劳神,多学英语,多用谷歌,很容易找到参考的,只要方向对
[mw_shl_code=csharp,true]using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace Name
{
public class FileIO
{

[DllImport("api-ms-win-core-file-l1-2-1.dll", CharSet = System.Runtime.InteropServices.CharSet.Unicode, ExactSpelling = false, PreserveSig = true, SetLastError = true)]
internal static extern bool GetFileAttributesEx(string name, int fileInfoLevel, ref WIN32_FILE_ATTRIBUTE_DATA lpFileInformation);

[StructLayout(LayoutKind.Sequential)]
private struct WIN32_FILE_ATTRIBUTE_DATA
{
public Windows.Storage.FileAttributes Attributes;
public System.Runtime.InteropServices.ComTypes.FILETIME CreationTime;
public System.Runtime.InteropServices.ComTypes.FILETIME LastAccessTime;
public System.Runtime.InteropServices.ComTypes.FILETIME LastWriteTime;
public int SizeHigh;
public int SizeLow;
}

private bool Exist(string path)
{
WIN32_FILE_ATTRIBUTE_DATA data = new WIN32_FILE_ATTRIBUTE_DATA();

GetFileAttributesEx(path, 0, ref data);

if (data.Attributes == Windows.Storage.FileAttributes.Archive || data.Attributes == Windows.Storage.FileAttributes.Directory)
return true;
return false;

}
}
[/mw_shl_code]

记住,这里的path要使用绝对地址。以后就可以舒心的使用了。

非常感谢后面的人提醒,要使用 windows 10 api sets, 或者 windows 8.1 api sets
windows 8.1
api-ms-win-core-file-l1-2-1.dll
CompareFileTime
CreateDirectoryA
CreateDirectoryW
CreateFile2
CreateFileA
CreateFileW
DefineDosDeviceW
DeleteFileA
DeleteFileW
DeleteVolumeMountPointW
FileTimeToLocalFileTime
FindClose
FindCloseChangeNotification
FindFirstChangeNotificationA
FindFirstChangeNotificationW
FindFirstFileA
FindFirstFileExA
FindFirstFileExW
FindFirstFileW
FindFirstVolumeW
FindNextChangeNotification
FindNextFileA
FindNextFileW
FindNextVolumeW
FindVolumeClose
FlushFileBuffers
GetCompressedFileSizeA
GetCompressedFileSizeW
GetDiskFreeSpaceA
GetDiskFreeSpaceExA
GetDiskFreeSpaceExW
GetDiskFreeSpaceW
GetDriveTypeA
GetDriveTypeW
GetFileAttributesA
GetFileAttributesExA
GetFileAttributesExW
GetFileAttributesW
GetFileInformationByHandle
GetFileSize
GetFileSizeEx
GetFileTime
GetFileType
GetFinalPathNameByHandleA
GetFinalPathNameByHandleW
GetFullPathNameA
GetFullPathNameW
GetLogicalDrives
GetLogicalDriveStringsW
GetLongPathNameA
GetLongPathNameW
GetShortPathNameW
GetTempFileNameW
GetTempPathA
GetVolumeInformationByHandleW
GetVolumeInformationW
GetVolumeNameForVolumeMountPointW
GetVolumePathNamesForVolumeNameW
GetVolumePathNameW
LocalFileTimeToFileTime
LockFile
LockFileEx
QueryDosDeviceW
ReadFile
ReadFileEx
ReadFileScatter
RemoveDirectoryA
RemoveDirectoryW
SetEndOfFile
SetFileAttributesA
SetFileAttributesW
SetFileInformationByHandle
SetFileIoOverlappedRange
SetFilePointer
SetFilePointerEx
SetFileTime
SetFileValidData
UnlockFile
UnlockFileEx
WriteFile
WriteFileEx
WriteFileGather

api-ms-win-core-file-l2-1-1.dll
CopyFile2
CopyFileExW
CreateDirectoryExW
CreateHardLinkW
CreateSymbolicLinkW
GetFileInformationByHandleEx
MoveFileExW
MoveFileWithProgressW
OpenFileById
ReadDirectoryChangesW
ReOpenFile
ReplaceFileW

标签: 开发 课堂

敬告:
为防止不可控的内容风险,本站已关闭新用户注册,新贴的发表及评论;
你现在看到的内容只是互联网用户曾经发表的言论快照,仅用于老用户留存纪念,且仅与科技行业相关,全部内容不代表本站观点及立场;
本站重新开放前已针对包括用户隐私、版权保护、信息安全、国家政策在内的各种互联网法律法规要求,执行了隐患内容的自查、屏蔽和删除;
本站目前所属个人主体,未有任何盈利安排与计划,且与原WFUN.COM所属公司不存在任何关联关系;
如果本帖内容或者相关资源侵犯到您的合法权益,或者您认为存在问题,那么请您务必点此举报或投诉!
全部回复:
望****阁 UID.508304
2015-05-16 回复

楼主有没有比较好的学习视频呀?看了传智播课的,不知道怎么学下去了,非专业人员,个人爱好,

qiqiminmin UID.638527
2015-05-16 回复

本帖最后由 qiqiminmin 于 2015-5-16 21:03 编辑

Quote***链接停止解析***
楼主有没有比较好的学习视频呀?看了传智播课的,不知道怎么学下去了,非专业人员,个人爱好, ...


我自己基本上是看开发文档,国内的开发视频,确实做得很差,和国外的差太远了。可能还是按照官方指导一步一步做

tmp00000 UID.995403
2015-05-17 回复

然而并没有什么用。应用程序包验证过不了。

tmp00000 UID.995403
2015-05-17 回复

用那个api就过不了认证,交到商店估计会被退下来。

tmp00000 UID.995403
2015-05-17 回复

我查了一下资料,楼主把dll名称写错了。
api-ms-win-core-file-l1-2-0.dll
是正确的。
Declare Unicode Function GetFileAttributesExW Lib "api-ms-win-core-file-l1-2-0.dll" (<MarshalAs(UnmanagedType.LPWStr)> name As String, fileInfoLevel As Integer, ByRef lpFileInformation As WIN32_FILE_ATTRIBUTE_DATA) As Boolean

Structure WIN32_FILE_ATTRIBUTE_DATA
Dim Attributes As FileAttributes
Dim CreationTime As ComTypes.FILETIME
Dim LastAccessTime As ComTypes.FILETIME
Dim LastWriteTime As ComTypes.FILETIME
Dim SizeHigh As Integer
Dim SizeLow As Integer
End Structure
Function FileOrDirExists(path As String) As Boolean
Dim data As New WIN32_FILE_ATTRIBUTE_DATA()
GetFileAttributesExW(path, 0, data)
Return data.Attributes = FileAttributes.Archive OrElse data.Attributes = FileAttributes.Directory
End Function

qiqiminmin UID.638527
2015-05-17 回复

本帖最后由 qiqiminmin 于 2015-5-17 23:54 编辑

Quote***链接停止解析***
我查了一下资料,楼主把dll名称写错了。
api-ms-win-core-file-l1-2-0.dll
是正确的。


哦,如果是这样,那真是太感谢l。。。。。我马上改改。虽然不是什么大问题,但是在windows store 上开发,都需要改这个改那个的,通常是名称,幸运的是GetFileAttributes是支持windows phone 8.1, windows phone 8的,如果不支持,我们想弄也弄不了,真希望微软更加完善一些。

以****p UID.1027271
2015-06-20 回复

内容已屏蔽

本站使用Golang构建,点击此处申请开源鄂ICP备18029942号-4联系站长投诉/举报