计算Windows Phone上文件夹中的文件数

时间:2013-10-22 20:42:21

标签: windows-phone-7 windows-phone

我有一个包含许多.wav文件的文件夹。我需要动态计算这些文件,以便以随机的方式生成声音。我需要动态,所以每次用另一种声音更新声音文件夹时我都不需要更新代码。 我搜索了如何做,但只能找到Windows的例子。这是我提出的代码:

string path = string.Format("/Sound/{0}", sourceSound);
return Directory.GetFiles(path, ".wav").Length;

我尝试过运行它,但VS给了我错误:

“无法步骤。代码目前无法使用。”

我有什么问题,或者其他任何情况我们如何计算文件夹中的文件数量?

感谢。

2 个答案:

答案 0 :(得分:0)

尝试

using (var myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
        {
            var i = myIsolatedStorage.GetFileNames("*.wav").Length;
        }

答案 1 :(得分:0)

出于某种原因,我不能让Vitalii的例子让我工作。我无法获得我在volder中拥有的文件数。 看着互联网,我偶然发现了这个链接:

confused about resources and GetManifestResourceNames()

Zack在这个帖子中给出了答案,给出了使我的应用程序工作所需的洞察力。

我使用Embedded Resource查找我需要的所有文件数,然后使用SoundEffectInstance进行播放。 以下链接对此有所帮助:

http://matthiasshapiro.com/2011/01/10/embedding-a-sound-file-in-windows-phone-7-app-silverlight/

以下是我设法使其发挥作用的方式:

soundCount = GetSoundCount();

private int GetSoundCount()
        {
            return Assembly.GetExecutingAssembly().GetManifestResourceNames().Where(name => name.Contains(sourceImg)).Count();
        }

通过这几行,我设法获得了我的应用程序中的确切文件数。

为了播放声音,我使用第二个链接作为示例并生成以下代码:

if(soundInstance != null)
soundInstance.Stop();
this.soundInstance = SetNextSource();
soundInstance.Play();


private SoundEffectInstance SetNextSource()
        {
            Random random = new Random();
            Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly();
            Stream stream = assembly.GetManifestResourceStream(string.Format("Assembly Name.Folder.{0}.{1}.wav", SubFolder, FileName + random.Next(soundCount)));
            SoundEffect se = SoundEffect.FromStream(stream);
            return se.CreateInstance();
        }

好几天后,研究终于成功了。 希望这个主题可以帮助那些面临同样问题的人。

感谢。

相关问题