Enumerare un elenco di file in una directory specificata
					Pagina aggiornata : 
					
				
				Data di creazione della pagina : 
			sommario
Enumera un elenco di file nella directory specificata.
Ambiente operativo
Prerequisiti
| Versioni XNA supportate | 
 | 
| Piattaforme supportate | 
 | 
| Versione Vertex Shader richiesta da Windows | 1.1 | 
| Versione Pixel Shader richiesta da Windows | 1.1 | 
Ambiente operativo
| piattaforma | 
Come utilizzare l'esempio
| Funziona con tastieraController Xbox | 360mouse | ||
|---|---|---|---|
| Selezionare un dispositivo di archiviazione per ottenere ed enumerare l'elenco dei file | Un | Un | - | 
sostanza
Ottenere un elenco di file nella cartella specificata
Per ottenere un elenco di file in una cartella specificata, utilizzare il metodo "Directory.GetFiles". Il primo argomento è il percorso della cartella da cui si desidera ottenere l'elenco dei file. Il valore restituito è una matrice di percorsi di file per il numero di file.
// ストレージコンテナを開きます
using (StorageContainer container = storageDevice.OpenContainer("XNASample"))
{
    // ファイルリストをクリア
    this.enumerateFileList.Clear();
    // 指定したディレクトリのファイル名一覧を取得する
    this.enumerateFileList.AddRange(Directory.GetFiles(container.Path));
}
Directory.GetFiles metodo
Restituisce un elenco di percorsi di file nella directory specificata.
| sentiero | corda | Directory da cui recuperare i file. | 
| Valori restituiti | stringa[] | Matrice String di nomi di file nella directory specificata. Il nome del file include il percorso completo. | 
Nell'esempio viene visualizzato un elenco di file creati da Suggerimenti, ad esempio "Salva dati" e "Crea, duplica, elimina, sposta e rinomina file".
Tutti i codici
using System;
using System.Collections.Generic;
using System.IO;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
namespace EnumerateFiles
{
     <summary>
     ゲームメインクラス
     </summary>
    public class GameMain : Microsoft.Xna.Framework.Game
    {
         <summary>
         グラフィックデバイス管理クラス
         </summary>
        private GraphicsDeviceManager graphics = null;
         <summary>
         スプライトのバッチ化クラス
         </summary>
        private SpriteBatch spriteBatch = null;
         <summary>
         スプライトでテキストを描画するためのフォント
         </summary>
        private SpriteFont font = null;
         <summary>
         列挙されたファイルリスト
         </summary>
        private List<string> enumerateFileList = new List<string>();
         <summary>
         直線のキーボード入力の状態
         </summary>
        private KeyboardState oldKeyboardState = new KeyboardState();
         <summary>
         直線のゲームパッド入力の状態
         </summary>
        private GamePadState oldGamePadState = new GamePadState();
         <summary>
         GameMain コンストラクタ
         </summary>
        public GameMain()
        {
            // グラフィックデバイス管理クラスの作成
            this.graphics = new GraphicsDeviceManager(this);
            // ゲームコンテンツのルートディレクトリを設定
            this.Content.RootDirectory = "Content";
            // ゲームサービスコンポーネントを追加
            this.Components.Add(new GamerServicesComponent(this));
        }
         <summary>
         ゲームが始まる前の初期化処理を行うメソッド
         グラフィック以外のデータの読み込み、コンポーネントの初期化を行う
         </summary>
        protected override void Initialize()
        {
            // TODO: ここに初期化ロジックを書いてください
            // コンポーネントの初期化などを行います
            base.Initialize();
        }
         <summary>
         ゲームが始まるときに一回だけ呼ばれ
         すべてのゲームコンテンツを読み込みます
         </summary>
        protected override void LoadContent()
        {
            // テクスチャーを描画するためのスプライトバッチクラスを作成します
            this.spriteBatch = new SpriteBatch(this.GraphicsDevice);
            // フォントをコンテンツパイプラインから読み込む
            this.font = this.Content.Load<SpriteFont>("Font");
        }
         <summary>
         ゲームが終了するときに一回だけ呼ばれ
         すべてのゲームコンテンツをアンロードします
         </summary>
        protected override void UnloadContent()
        {
            // TODO: ContentManager で管理されていないコンテンツを
            //       ここでアンロードしてください
        }
         <summary>
         描画以外のデータ更新等の処理を行うメソッド
         主に入力処理、衝突判定などの物理計算、オーディオの再生など
         </summary>
         <param name="gameTime">このメソッドが呼ばれたときのゲーム時間</param>
        protected override void Update(GameTime gameTime)
        {
            // キーボードの情報取得
            KeyboardState keyboardState = Keyboard.GetState();
            // ゲームパッドの情報取得
            GamePadState gamePadState = GamePad.GetState(PlayerIndex.One);
            // Xbox360 コントローラの BACK ボタンを押したときにゲームを終了させます
            if (gamePadState.Buttons.Back == ButtonState.Pressed)
            {
                this.Exit();
            }
            if ((keyboardState.IsKeyDown(Keys.A) && this.oldKeyboardState.IsKeyUp(Keys.A)) ||
                (gamePadState.Buttons.A == ButtonState.Pressed &&
                    this.oldGamePadState.Buttons.A == ButtonState.Released))
            {
                // A ボタンが押されたとき /////
                // ストレージデバイス選択UIを表示するための設定を行います
                Guide.BeginShowStorageDeviceSelector(this.GetStorageDevice, null);
            }
            // 入力情報を記憶
            this.oldKeyboardState = keyboardState;
            this.oldGamePadState = gamePadState;
            // 登録された GameComponent を更新する
            base.Update(gameTime);
        }
         <summary>
         ストレージデバイスを取得するために呼ばれる
         </summary>
         <param name="result">非同期処理の結果</param>
        private void GetStorageDevice(IAsyncResult result)
        {
            // 結果をもとにストレージデバイスの選択UIを終了してストレージデバイスを取得します
            StorageDevice storageDevice = Guide.EndShowStorageDeviceSelector(result);
            if (storageDevice != null && storageDevice.IsConnected)
            {
                // ストレージデバイスの取得に成功し、接続されている場合 /////
                // ストレージコンテナを開きます
                using (StorageContainer container = storageDevice.OpenContainer("XNASample"))
                {
                    // ファイルリストをクリア
                    this.enumerateFileList.Clear();
                    // 指定したディレクトリのファイル名一覧を取得する
                    this.enumerateFileList.AddRange(Directory.GetFiles(container.Path));
                }
            }
        }
         <summary>
         描画処理を行うメソッド
         </summary>
         <param name="gameTime">このメソッドが呼ばれたときのゲーム時間</param>
        protected override void Draw(GameTime gameTime)
        {
            // 画面を指定した色でクリアします
            this.GraphicsDevice.Clear(Color.CornflowerBlue);
            // スプライトの描画準備
            this.spriteBatch.Begin();
            // テキスト描画
            this.spriteBatch.DrawString(this.font,
                "A : Select Storage Device.",
                new Vector2(50.0f, 50.0f), Color.White);
            // ファイルのリストを表示
            string text = "[FileList]\r\n";
            foreach (string file in this.enumerateFileList)
            {
                text += file + "\r\n";
            }
            this.spriteBatch.DrawString(this.font, text,
                new Vector2(50.0f, 80.0f), Color.White);
            // スプライトの一括描画
            this.spriteBatch.End();
            // 登録された DrawableGameComponent を描画する
            base.Draw(gameTime);
        }
    }
}