2013-10-17 5 views
0

문자열 변수를 .txt 파일의 특정 줄과 동일하게 설정하는 방법을 알아 내려고 할 때 작은 문제가 있습니다. 내가하려는 것은 ContactInformatiom.ContactName으로 설정하고 ContactInformation의 모든 내용은 정의를 저장 한 .txt 파일의 다른 줄과 같지만 그 방법을 알 수는없는 것 같습니다. 나는 모든 종류의 일을 시도하고 그들 중 누구도 작동하지 않는 것 같아요, 나는이 웹 사이트와 다른 곳곳을 들여다 보았고 자신과 같은 문제를 가진 사람을 만나지 않았습니다. 그게 뭐라는거야? 당신이 다른 어떤 정보 나 내가 부탁 해요 무엇의 설명이 필요한 경우문자열을 .txt 파일의 줄과 동일하게 설정하십시오.

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace Contact_List 
{ 
public class ContactInformation 
{ 
    public static string ContactName; 
    public static string ContactCellNumber; 
    public static string ContactOtherNumber; 
    public static string ContactRelationship; 
    public static string ContactEmail; 
    public static string ContactNote; 
    } 
} 

, 물어 주시기 바랍니다 :

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 
using System.IO; 

namespace Contact_List 
{ 
public partial class Form1 : Form 
{ 

    string contactpath = @"CurrentContacts.txt"; 
    string contactpath2 = @"" + ContactInformation.ContactName + ".txt"; 
    public Form1() 
    { 
     if(!File.Exists(contactpath)) 
     { 
      File.Create(contactpath, 1024).Dispose(); 
     } 
     InitializeComponent(); 
    } 

    private void AddContactButton_Click(object sender, EventArgs e) 
    { 
     NewContactTextBoxName.Visible = true; 
     NewContactTextBoxCell.Visible = true; 
     NewContactName.Visible = true; 
     NewContactPhoneNumberOther.Visible = true; 
     NewContactSaveButton.Visible = true; 
     NewContactSaveText.Visible = true; 
     textBox6.Visible = true; 
     textBox5.Visible = true; 
     textBox4.Visible = true; 
    } 
    private void NewContactSaveButton_Click_1(object sender, EventArgs e) 
    { 
     if (NewContactTextBoxName.Text == "") 
     { 
      MessageBox.Show("Please enter a Contact Name"); 
     } 
     else 
     { 
      ContactInformation.ContactName = NewContactTextBoxName.Text; 
      string contactpath2 = @"" + ContactInformation.ContactName + ".txt"; 
      string Readfile = File.ReadAllText(contactpath); 
      if (Readfile.Contains(NewContactTextBoxName.Text)) 
      { 
       MessageBox.Show("This contact already exsists. Please rename the contact you're trying to create or delete the exsisting contact"); 
      } 
      else 
      { 
       comboBox1.Items.Add(ContactInformation.ContactName); 
       File.Create(contactpath2).Dispose(); 
       using (StreamWriter Writeline = new StreamWriter(contactpath, true)) 
       { 
       Writeline.WriteLine(ContactInformation.ContactName); 
       Writeline.Dispose(); 
       } 
       using (StreamWriter Writeline2 = new StreamWriter(contactpath2, true)) 
       { 
       Writeline2.WriteLine(NewContactTextBoxName.Text); 
       Writeline2.WriteLine(NewContactTextBoxCell.Text); 
       Writeline2.WriteLine(NewContactPhoneNumberOther.Text); 
       Writeline2.WriteLine(textBox6.Text); 
       Writeline2.WriteLine(textBox5.Text); 
       Writeline2.WriteLine(textBox4.Text); 
       Writeline2.Dispose(); 
       } 
       NewContactName.Visible = false; 
       NewContactPhoneNumberOther.Visible = false; 
       NewContactSaveButton.Visible = false; 
       NewContactSaveText.Visible = false; 
       textBox6.Visible = false; 
       textBox5.Visible = false; 
       NewContactTextBoxName.Visible = false; 
       NewContactTextBoxCell.Visible = false; 
       textBox4.Visible = false; 


      } 
     } 
    } 

    private void comboBox1_SelectionChangeCommitted(object sender, EventArgs e) 
    { 
    string currentselection = @"" + comboBox1.SelectedItem.ToString() + ".txt"; 
    File.ReadAllLines(currentselection); 
    //set Contactinformation.ContactName = to line one of the .txt file 
    //set ContactInformation.ContactCell = to line two of the .txt file 
    //etc. 



     } 
    } 
} 

및 다음 클래스 :

나는 다음과 같은 코드를 사용하고 있습니다 내가 뭘 하려는지 충분히 명확히 알 수 없다면 미리 미안해.

답변

1

시도 :

string[] contactInfo = File.ReadAllLines(currentselection); 

Contactinformation.ContactName = contactInfo[0]; 
Contactinformation.ContactCell = contactInfo[1]; 
. 
. 
. 
. 
. 

당신이

+0

가 무리 감사 요소에 액세스하기 전에 배열의 길이를 확인해야합니다! 대단 했어! – Shaquanda

관련 문제