简体   繁体   中英

How to parse a file into a list of string to use for auto-complete in a windows forms application in C#?

I am building an application in C# that has a textbox field. In this field, a user will write text and the text will autocomplete from a file found on a remote repository. I am using a library called SharpSVN and I am trying to find a method where I can fetch that file from the repository based on a certain path I provide, then parse the content into strings that will be added to the list in the autocomplete of the textbox mentioned previously.

There are two ways:

  1. Download the file text using the repository url. If you want the file at a specific revision, try entering in "?r=12345" as to get the file's appearance at a specific revision number:

     string fileText = new WebClient().DownloadFile("https://myrepo.com/myfile.txt", localFilename); 
  2. Or, you could also use SharpSVN, removing the revision options if you want the latest version:

     public string GetFileContentsAsString(long revisionNumber) { return new StreamReader(GetFileContents(revisionNumber)).ReadToEnd(); } private MemoryStream GetFileContents(long revisionNumber) { SvnRevision rev = new SvnRevision(revisionNumber); MemoryStream stream = new MemoryStream(); using (SvnClient client = GetClient()) { client.FileVersions(SvnTarget.FromUri(RemotePath), new SvnFileVersionsArgs() { Start = rev, End = rev }, (s, e) => { e.WriteTo(stream); }); } stream.Position = 0; return stream; } 

When you have the file as a text string, you can use .NET's String.Split() method to split the text into a list of lines using the '\\n' line-break character as the delimiter:

string[] fileAsLines = fileText.Split(new char[] {'\n'});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM