-2

I have two separate questions:

a) How to remove .mp3 / .mpeg extensions from multiple selected files
b) When the multiple files are added to the listbox. How can I set a max character length (50 chars)

String[] files, paths;
private void addbutton_Click(object sender, EventArgs e)
{
    if (openFileDialog1.ShowDialog() == DialogResult.OK)
    {
        files = openFileDialog1.SafeFileNames;
        paths = openFileDialog1.FileNames;
        for (int x = 0; x < files.Length; x++)
        {
            listBox1.Items.Add(files[x]);
            // How can I remove the file extentions here? I know I can't use substring right?
            // Only use 50 chars after chopping off the extentions
        }
    }
}

I searched google but there are no answers relating to multiple files. Thanks guys!

Mohamad Shiralizadeh
  • 8,329
  • 6
  • 58
  • 93
Robl0x
  • 3
  • 3
  • 1
    `I searched google but there are no answers relating to multiple files.` Because it is very unique problem, no one has faced before. – L.B Nov 26 '14 at 21:08

2 Answers2

0

How to remove .mp3 / .mpeg extensions from multiple selected files

You can use Path.GetFileNameWithoutExtension method to get the file name without extension:

var fileName = Path.GetFileNameWithoutExtension("path");

b) When the multiple files are added to the listbox. How can I set a max character length (50 chars)

Use Substring

fileName = fileName.Substring(0, 50);
Selman Genç
  • 100,147
  • 13
  • 119
  • 184
0

You can use Path.GetFileNameWithoutExtension and String.Remove:

string fileWithOutExtension = System.IO.Path.GetFileNameWithoutExtension(files[x]);
if (fileWithOutExtension.Length > 50)
    fileWithOutExtension = fileWithOutExtension.Remove(50);
listBox1.Items.Add(fileWithOutExtension);
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939