2010-04-11 2 views
2

위치 문자열에서 텍스트 파일을 읽는 대신 리소스 위치에서 텍스트 파일을 읽도록 변경하고 문제가 발생합니다. 내 프로그램. 또한 삽입 코드 조각 방법을 사용하여이 코드의 대부분을 얻었으므로 어떤 일이 일어나고 있는지 모르겠습니다. 어떤 사람이 도와 주실 수 있습니까?리소스에서 구분 된 텍스트 파일을 읽으려고 시도했습니다. 그러나 실행되지 않습니다.

'reads the text out of a delimited text file and puts the words and hints into to separate arrays 
    ' this works and made the program run 
    ' Dim filename As String = Application.StartupPath + "\ProggramingList.txt" 
    'this dosnt work and brings back a Illegal characters in path error. 
    dim filename as string = My.Resources.ProggramingList 
    Dim fields As String() 
    'my text files are delimited 
    Dim delimiter As String = "," 
    Using parser As New TextFieldParser(filename) 
     parser.SetDelimiters(delimiter) 
     While Not parser.EndOfData 
      ' Read in the fields for the current line 
      fields = parser.ReadFields() 
      ' Add code here to use data in fields variable. 

      'put the result into two arrays (the fields are the arrays im talking about). one holds the words, and one holds the corresponding hint 
      Programingwords(counter) = Strings.UCase(fields(0)) 
      counter += 1 
      'this is where the hint is at 
      Programingwords(counter) = (fields(1)) 
      counter += 1 
     End While 
    End Using 

오류

ex.ToString()
"System.ArgumentException :. 경로에 잘못된 문자 System.IO.Path.CheckInvalidPathChars에서 (문자열 경로) System.IO.Path에서 (문자열 경로, 부울 전체 검사) 에서 System.IO.Path.NormalizePath (문자열 경로, 부울 전체 검사) at System.IO.Path.GetFullPathInternal (문자열 경로) at System.IO.Path.GetFullPath (문자열 경로, 부울 전체 검사)) (Microsoft.VisualBasic.FileIO.FileSystem.Nor) Microsoft.VisualBasic.FileIO.TextFieldParser에서 Microsoft.VisualBasic.FileIO.TextFieldParser.InitializeFromPath (문자열 경로, 인코딩 defaultEncoding, 부울 detectEncoding) 에서 Microsoft.VisualBasic.FileIO.TextFieldParser.ValidatePath (문자열 경로) 에서 malizePath (문자열 경로) ..ctor I에서 (문자열 경로) HangMan.Form1.GetWords에서 () : \ VB \ 교수형 집행 \ 교수형 집행 인 \를 Form1.vb :이 코드를 디버깅 할 때 라인 (274)은 "문자열은 값은 무엇인가

+0

'My.Resources.GamesList'가 설정된 곳에 게시 할 수 있습니까? –

+0

내가 리소스 폴더를 추측하고 있습니까? 방금 텍스트 파일을 프로그램 리소스에 추가했습니다. 이 일을하는 유일한 이유는 내 프로그램이 .exe 파일에서 실행될 수 있기 때문입니다. – Bigfatty

+0

죄송합니다. 내 주석 처리되지 않은 코드가 혼동을 일으키고 있다면 사실입니다. 두 파일의 이름은 proggramingList – Bigfatty

답변

2

사용하는 TextFieldParser 생성자에는 파일 이름이 필요합니다. 대신 파일 내용을 가져옵니다. Kaboom은 파일 내용이 파일의 유효한 경로가 아닙니다. Stream을 사용하고 StringReader 클래스를 사용하여 스트림을 제공하는 생성자가 필요합니다. 예를 들면 다음과 같습니다.

Dim fields As String() 
Dim delimiter As String = "," 
Dim fileContent As String = My.Resources.ProggramingList 
Dim stringStream as New System.IO.StringReader(fileContent) 
Using parser As New TextFieldParser(stringStream) 
    REM etc... 
End Using 

텍스트가 메가 바이트보다 작 으면 메모리는 낭비되지만 문제는 없습니다. 그 이상인 경우 자원에 넣으면 안됩니다.

+0

대단히 감사합니다! 무슨 일이 일어나고 있는지 나에게 설명해 주셔서 감사합니다. 꼭해야만하는 것처럼 작동합니다! 너는 남자 야! – Bigfatty

+0

나는 그것이 도움이 될만큼 충분히 높은 순위에 있지 않다. 미안, 나는 시도했다 : ( – Bigfatty

+0

고마워, 나는 내가 그것을 돌려주기 위해 돌아올 것을 확실히 할 것이다, 당신은 나를 많이 도왔다. – Bigfatty

0

변수 파일 이름을 My.Resources.GamesList에서 읽은 후에? 유효한 문자열인지, 파일인지를 가리 킵니까?

+0

이어야합니다. 아주 긴 문자열. 내가 게시 할 수있는 가치가 필요하다면 – Bigfatty

관련 문제