Tatia Posted March 9, 2015 at 06:39 PM Report Share #579032 Posted March 9, 2015 at 06:39 PM (edited) Boas amigos, Estou a tentar encontrar uns valores em ficheiro de texto e apresentar numa listbox, porem ele s贸 apresenta o primeiro valor encontrado. Actualmente para teste tenho o seguinte em um ficheiro de texto: -rw------- 12354 /Pasta/data/arquivo.rar -rw------- 265584 /Pasta/data2/arquivo.rar -rw------- 262160 /Pasta/data3/data3/arquivo.zip E eu pretendo apenas que seja apresentado na listbox o seguinte: /Pasta/data/arquivo.rar /Pasta/data2/arquivo.rar /Pasta/data3/data3/arquivo.zip Meu c贸digo de teste utilizado esta assim: using System; using System.IO; using System.Text.RegularExpressions; using System.Windows.Forms; namespace Teste1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { string Texto = File.ReadAllText("out.txt"); Match match = Regex.Match(Texto, @"/Pasta/([\w-\d]*.*)", RegexOptions.Multiline); if (match.Success) { listBox1.Items.Add(match.Groups[0].Value); } } private void button2_Click(object sender, EventArgs e) { listBox1.Items.Clear(); } } } --Edite-- Tentei algo assim que acho que seria o mais correcto mas sem sucesso. 馃槥 string input = File.ReadAllText("out.txt"); string pattern = @"/Pasta/([\w-\d]*.*)"; MatchCollection matches = Regex.Matches(input, pattern); foreach (Match match in matches) { try { listBox1.Items.Add(match.Groups[0].Value); } catch { } } Edited March 9, 2015 at 07:03 PM by Tatia Link to comment Share on other sites More sharing options...
bioshock Posted March 9, 2015 at 07:36 PM Report Share #579035 Posted March 9, 2015 at 07:36 PM (edited) var matches = Regex.Matches(Texto, @"/Pasta/([\w-\d]*.*)", RegexOptions.Multiline); foreach (Match m in matches) { listBox1.Items.Add(m.Value); } Edited March 9, 2015 at 07:36 PM by bioshock 1 Report Link to comment Share on other sites More sharing options...
Tatia Posted March 10, 2015 at 06:56 PM Author Report Share #579088 Posted March 10, 2015 at 06:56 PM Obrigado amigo... Mas onde estava eu a errar aqui neste exemplo? string input = File.ReadAllText("out.txt"); string pattern = @"/Pasta/([\w-\d]*.*)"; MatchCollection matches = Regex.Matches(input, pattern); foreach (Match match in matches) { try { listBox1.Items.Add(match.Groups[0].Value); } catch { } Link to comment Share on other sites More sharing options...
bioshock Posted March 10, 2015 at 07:25 PM Report Share #579094 Posted March 10, 2015 at 07:25 PM Falta-te fechar uma chaveta, a menos que n茫o tenhas copiado todo o c贸digo correctamente. De resto tamb茅m funciona. O exemplo que te apresentei foi aquele porque era o que eu j谩 tinha a m茫o fazendo a substitui莽茫o do devido pattern e porque n茫o tinha oportunidade de testar o teu c贸digo. 1 Report Link to comment Share on other sites More sharing options...
Tatia Posted March 11, 2015 at 07:59 PM Author Report Share #579174 Posted March 11, 2015 at 07:59 PM (edited) Falta-te fechar uma chaveta, a menos que n茫o tenhas copiado todo o c贸digo correctamente. De resto tamb茅m funciona. O exemplo que te apresentei foi aquele porque era o que eu j谩 tinha a m茫o fazendo a substitui莽茫o do devido pattern e porque n茫o tinha oportunidade de testar o teu c贸digo. Ate tinha a chaveta, mas n茫o dava... Bom agora esta resolvido muito obrigado... Edite: Ja agora ap贸s ter na listbox o valor seguinte: /Pasta/data/arquivo.rar Como obtenho apenas o nome e extens茫o do ficheiro? Neste caso seria. arquivo.rar O que estou a fazer e seleccionar o ficheiro na listbox e guardar o arquivo em outro local. Ja esta tudo a trabalhar so que tenho de ter o nome do ficheiro definido, pretendo usar o nome que e apresentado na listbox para guardar o ficheiro... Um abra莽o Edited March 12, 2015 at 12:31 AM by Tatia Link to comment Share on other sites More sharing options...
bioshock Posted March 12, 2015 at 06:55 PM Report Share #579243 Posted March 12, 2015 at 06:55 PM (edited) string nome = System.IO.Path.GetFileName(match.Value); string nomeSemExt = System.IO.Path.GetFileNameWithoutExtension(match.Value); string ext = System.IO.Path.GetExtension(match.Value); Edited March 12, 2015 at 06:57 PM by bioshock 1 Report Link to comment Share on other sites More sharing options...
Tatia Posted March 12, 2015 at 08:21 PM Author Report Share #579258 Posted March 12, 2015 at 08:21 PM string nome = System.IO.Path.GetFileName(match.Value); string nomeSemExt = System.IO.Path.GetFileNameWithoutExtension(match.Value); string ext = System.IO.Path.GetExtension(match.Value); Podes colocar um exemplo de como usar seguindo o teu c贸digo? var matches = Regex.Matches(Texto, @"/Pasta/([\w-\d]*.*)", RegexOptions.Multiline); foreach (Match m in matches) { listBox1.Items.Add(m.Value); } Link to comment Share on other sites More sharing options...
bioshock Posted March 12, 2015 at 09:26 PM Report Share #579265 Posted March 12, 2015 at 09:26 PM Ent茫o, aonde 茅 que achas que deves colocar o c贸digo que te dei? Eu dei-te tr锚s exemplos para te possibilitar outras alternativas caso assim o queiras. Mas tudo o que pediste foi obter o nome do ficheiro com a respectiva extens茫o, pelo que deves utilizar o primeiro exemplo. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now