2009-07-13 9 views
1

내 프로그램이 타이머가 만료되면 임의의 임베디드 오디오 파일을 재생하므로 my.resources 파일을 배열로 참조하여 리소스의 정확한 파일 이름을 알 필요가없는 방식으로 사용할 수 있습니까? ?리소스가 있습니까?

+0

당신에게 로베르토 감사입니까? – 7wp

+0

윈도우 응용 프로그램, 기본적으로 화면 보호기 및 소리가 포함 된 eggtimer – Jim

답변

2

나는 Windows 응용 프로그램에서이 시도하지 않은,하지만 난 웹 응용 프로그램에 대해 같은 일을했고, 나는 그 달성하기 위해 다음 코드를 사용 :

VB 버전 :

' get a reference to the current assembly 
Dim a = Assembly.GetExecutingAssembly() 

' get a list of resource names from the manifest 
Dim resNames = a.GetManifestResourceNames() 

'Generate a random number 
Dim randomNumber = YourRandomNumberGenerator() 

'Get the file name using the randomNumber 
Dim randomFileNameFromResource = resNames(randomNumber) 

'Get the contents of the file: 
Dim sr = New StreamReader(a.GetManifestResourceStream(randomFileNameFromResource)) 
Dim fileContent = sr.ReadToEnd() 
을 내가 더 포함 된 리소스가 추가됩니다 경우 배열에있는 파일의 위치는하지만 시간이 지남에 따라 변경 것이라고 생각,하지만 난 그게 문제가되지 않을 거라 생각

 // get a reference to the current assembly 
     var a = Assembly.GetExecutingAssembly(); 

     // get a list of resource names from the manifest 
     var resNames = a.GetManifestResourceNames(); 

     //Generate a random number 
     var randomNumber = YourRandomNumberGenerator(); 

     //Get the file name using the randomNumber 
     var randomFileNameFromResource = resNames[randomNumber]; 

     //Get the contents of the file: 
     var sr = new StreamReader(
       a.GetManifestResourceStream(randomFileNameFromResource)); 
     var fileContent = sr.ReadToEnd(); 

C# 버전 어쨌든 무작위로 선택하기 때문에 당신을 위해.

+0

어셈블리는 vb2005에 직접 존재하지 않지만, My.Application.Info.LoadedAssemblies (0)라고 생각합니다 .GetFiles 거기서 ' t 너무 멀리 떨어져 – Jim

+0

아 감사합니다. .NET Framework v3.5에서 사용하고 있습니다. – 7wp

0

vb2005에서 나에 대한 정답은 Reflection.Assembly.GetExecutingAssembly.GetManifestResourceNames() 프로그래머는 GET 스트림 기능 거기에,이 웹 응용 프로그램 또는 Windows 응용 프로그램입니다

관련 문제