응용 프로그램 시작에서 File.ReadAllText ()를 사용하여 텍스트 파일을 저장하고 읽는 방법은 무엇입니까?

패트릭

응용 프로그램 시작시 텍스트 파일을 저장하고 응용 프로그램 시작시이 텍스트 파일을 읽습니다.

애플리케이션 시작시 파일이 저장되지 않습니다.이 코드에 어떤 문제가 있습니까?

응용 프로그램 시작 코드에서 텍스트 파일을 저장합니다.

private void Savebutton1_Click(object sender, EventArgs e)
    {
       StreamWriter sw = new StreamWriter(Application.StartupPath + "Book.txt", true);
        string json = JsonConvert.SerializeObject(vals);

            sw.Write(json);
            MessageBox.Show("Book Saved Successfully", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);

    }

응용 프로그램 시작 코드에서 텍스트 파일로드.

 string path = Path.Combine(Application.StartupPath, "ChequeBook.txt");
            string textholder;
            try
            {
                // Use StreamReader to consume the entire text file.
                using (StreamReader reader = new StreamReader(path))
                {
                    MessageBox.Show("Reached Here");
                    textholder = reader.ReadToEnd();
                    MessageBox.Show("Reached Here - 2");
                }

                if (textholder == string.Empty) {

                    return;
                }


                // Deserialise it from Disk back to a Dictionary
                string jsonToRead = File.ReadAllText(textholder);

                List<KeyValuePair<int, string>> myDictionaryReconstructed =
                    JsonConvert.DeserializeObject<List<KeyValuePair<int, string>>>(jsonToRead);
패트릭

File.CreateText 메서드를 사용하여 응용 프로그램 폴더에 텍스트 파일을 저장하고 작성하고 싶었습니다.

첫 번째 답변을 찾았습니다.

이것은 응용 프로그램 폴더에 텍스트 파일을 쓰고 저장합니다.

        using (StreamWriter sw = File.CreateText(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Book.txt")))
        {

            string json = JsonConvert.SerializeObject(vals);
            sw.Write(json);
        }
        MessageBox.Show("Book Saved Successfully", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);

응용 프로그램 폴더에서 텍스트 파일을 읽고 싶었습니다.

이것은 응용 프로그램 폴더에서 텍스트 파일의 내용을 읽습니다.

string jsonToRead = File.ReadAllText(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Book.txt"));

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

Related 관련 기사

뜨겁다태그

보관