【萬里征程——Windows App開發】檔案&資料——獲取檔案屬性
這一節來看看獲取檔案屬性吧,可以獲取到檔名、型別、最近訪問時間等等屬性哦。
建立Button和TextBlock
下面這段程式碼呢,都很簡單。
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
<Button Width="200" Height="70" Name="btnGetProp" Content="獲取檔案屬性" Click="btnGetProp_Click"/>
<TextBlock Name="tBlockProp" Margin="12" Width="480" FontSize="30"/>
</StackPanel>
</Grid>
</Page>
在Click事件中,先獲取到圖片庫,當然了,你可以獲取其他地方,我電腦上的庫檔案中,就只有文件庫和圖片庫有檔案了。然後建立一個檔案查詢,最後將這些檔案都賦給files。這裡的var可謂是非常的強大啊。例項化一個StringBuilder物件來輔助輸出資訊那真是太完美不過了,我們在前面也常用到它。
var folder = KnownFolders.PicturesLibrary;
var fileQuery = folder.CreateFileQuery();
var files = await fileQuery.GetFilesAsync();
StringBuilder fileProperties = new StringBuilder();
for (int i = 0; i < files.Count; i++)
{
StorageFile file = files[i];
fileProperties.AppendLine("File name: " + file.Name);
fileProperties.AppendLine("File type: " + file.FileType);
BasicProperties basicProperties = await file.GetBasicPropertiesAsync();
string fileSize = string.Format("{0:n0}", basicProperties.Size);
fileProperties.AppendLine("File size: " + fileSize + " bytes");
fileProperties.AppendLine("Date modified: " + basicProperties.DateModified);
fileProperties.AppendLine(" ");
}
tBlockProp.Text = fileProperties.ToString();
這樣一來就完成對Name、FileType、Size和DateModified屬性的獲取,但還有一類屬性,則比較難以獲取,它們就是“擴充套件屬性”。
List<string> propertiesName = new List<string>();
propertiesName.Add("System.DateAccessed");
propertiesName.Add("System.FileOwner");
IDictionary<string, object> extraProperties = await file.Properties.RetrievePropertiesAsync(propertiesName);
var propValue = extraProperties[dateAccessedProperty];
if (propValue != null)
fileProperties.AppendLine("Date accessed: " + propValue);
propValue = extraProperties[fileOwnerProperty];
if (propValue != null)
fileProperties.AppendLine("File owner: " + propValue);
最後將fileProperties傳遞給TextBlock即可。
tBlockProp.Text = fileProperties.ToString();
最後除錯App,就會像下圖一樣了。
但是如你所見,檔名太長了卻無法自動換行,而且資料也顯示不完整。改改XAML中的TextBlock即可。TextWrapping屬性設定為Wrap,則可以換行;將TextBlock新增到ScrollViewer內則會顯示出一個滾動條。
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
<Button Width="200" Height="70" Name="btnGetProp" Content="獲取檔案屬性" Click="btnGetProp_Click"/>
<ScrollViewer>
<TextBlock Name="tBlockProp" Margin="12" Width="480" FontSize="30" TextWrapping="Wrap"/>
</ScrollViewer>
</StackPanel>
</Grid>
那麼這一節就結束咯。下一篇再見!共同努力。
感謝您的訪問,希望對您有所幫助。
歡迎大家關注或收藏、評論或點贊。
為使本文得到斧正和提問,轉載請註明出處:
http://blog.csdn.net/nomasp
相關文章
- PHP獲取檔案基本屬性的方法PHP
- 檔案屬性資訊
- 檔案屬性
- 檔案的屬性
- Linux檔案屬性Linux
- 檔案時間屬性
- 4.3.2.3 關於PDB$SEED資料檔案的屬性
- linux檔案相關命令 透過檔案獲取父資料夾名稱Linux
- nodejs獲取檔案資訊,判斷是檔案或目錄NodeJS
- Mac系統檔案屬性Mac
- Spring YAML與屬性檔案配置檔案對比 | BaeldungSpringYAML
- 2.5.4 為 SYSAUX 表空間指定資料檔案屬性UX
- 讀取資料夾檔案
- win10怎麼更改檔案屬性 win10怎樣修改檔案屬性Win10
- 遞迴獲取檔案列表遞迴
- node實現檔案屬性批量修改(檔名)
- Visual Studio 檔案 BuildAction 屬性值UILDA
- 屬性配置檔案詳解(2)
- Python3 - 獲取資料夾中的檔案列表Python
- nodejs遞迴資料夾獲取所有檔案路徑NodeJS遞迴
- 通過web url獲取檔案資訊Web
- Laravel file 上傳檔案資訊獲取Laravel
- laravel file上傳檔案資訊獲取Laravel
- webpack配置檔案中屬性的位置和資料結構Web資料結構
- SpringBoot專案中獲取配置檔案的配置資訊Spring Boot
- 使用yaml檔案讀取資料YAML
- spring cloud+spring boot 電子商務spring boot獲取配置檔案的屬性CloudSpring Boot
- node實現檔案屬性批量修改(時間屬性)
- JavaScript獲取檔案字尾名JavaScript
- php獲取xml檔案內容PHPXML
- 檔案屬性及find命令總結
- Linux 檔案屬性及詳細操作Linux
- 使用PHP獲取影像檔案的EXIF資訊PHP
- win10系統txt檔案怎麼改屬性_win10 txt檔案如何更改屬性Win10
- win10如何修改音樂檔案屬性_win10更改音樂檔案屬性的方法Win10
- win10如何去掉檔案隱藏屬性_win10檔案屬性怎麼去掉隱藏Win10
- Java中的獲取檔案的物理絕對路徑,和讀取檔案Java
- Pandas之EXCEL資料讀取/儲存/檔案分割/檔案合併Excel
- 一個Java檔案解決mybatis呼叫資料庫欄位與Java類檔案屬性不對應JavaMyBatis資料庫