【萬裡征程——Windows App開發】檔案&資料——擷取檔案屬性,萬裡征程app開發

來源:互聯網
上載者:User

【萬裡征程——Windows App開發】檔案&資料——擷取檔案屬性,萬裡征程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>

那麼這一節就結束咯。下一篇再見!共同努力。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.