Removing file extension before adding to listBox c# [duplicate] - c#

This question already has answers here:
Getting file names without extensions
(14 answers)
Closed 6 years ago.
I'm currently creating a note taking application. I'm trying to add items to a listbox without their file extensions, I have tried GetAllFilesWithoutExtensions, but no luck. I'm currently able to add them but can't seem to remove the extension. Any advice would be greatly appreciated...
DirectoryInfo dir = new DirectoryInfo("../Debug/");
FileInfo[] files = dir.GetFiles("*.txt");
foreach (FileInfo file in files)
{
listBox1.Items.Add(file);
}

You can use LINQ and System.IO.Path.GetFileNameWithoutExtension
var fileNamesWithoutExtension = files
.Select(fi => System.IO.Path.GetFileNameWithoutExtension(fi.Name));
foreach(string fn in fileNamesWithoutExtension)
{
// ...
}

You could use System.IO.Path.GetFileNameWithoutExtension(string)
See MSDN

Create a class to help you:
class FInfo
{
public FileInfo Info { get; set; }
public FInfo (FileInfo fi) { Info = fi; }
public override string ToString()
{
return Path.GetFileNameWithoutExtension(Info.FullName);
}
}
Now fill the ListBox with instances of that class:
DirectoryInfo dir = new DirectoryInfo(yourPath);
FileInfo[] files = dir.GetFiles(yourFilter);
foreach (FileInfo file in files) listBox1.Items.Add(new FInfo(file));
Now you can access the full FileInfo data:
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
FInfo fi = listBox1.SelectedItem as FInfo;
if (fi != null) Console.WriteLine(fi.Info.FullName); // do your thing here!
}
And the ListBox will use the ToString method to display only the file names without path or extension..

Related

Read the contents of a listView, and display missing items in another listView based on the contents of a directory

I have a .NET webform that is displaying files found in a directory in a listView. This is the code for the display:
private void files()
{
try
{
DirectoryInfo dinfo = new DirectoryInfo(label2.Text);
FileInfo[] Files = dinfo.GetFiles("*.doc");
foreach (FileInfo file in Files)
{
listView1.Items.Add(file.Name);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
label2.Text contains the directory that houses the files. What I need is for a second listView to display a list of documents housed in another directory to display if the file does not appear in the first list view.
The second directory contains templates where as the first directory contains completed documents. The names are different in each directory, but they are similar. For example a completed document displayed in the first listView may be called DEFECT1_AA09890.doc. It's template may be called 05DEFECT.doc.
It is easy enough to display the contents of the template directory using this code:
private void templateDocuments()
{
string path = #"\\directoryname\foldername";
try
{
DirectoryInfo dinfo = new DirectoryInfo(path);
FileInfo[] Files = dinfo.GetFiles("*.doc");
foreach (FileInfo file in Files)
{
listView2.Items.Add(file.Name);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
But this does not compare contents and display based on the results.
Long story short, I want to display the contents of a directory in a listView, compare it to the contents of another directory, and display in a second listView what does not appear in the first.
Any help would be much appreciated.
Cheers.
Before adding file names to listView2, you need to check whether you already added them to listView1. One way of doing that is to store the files in listView1 in a HashSet<string>, then checking that before adding to listView2. Something like this should work:
private void filesAndTemplates()
{
string path = #"\\directoryname\foldername";
HashSet<string> files = new HashSet<string>();
try
{
DirectoryInfo dinfo = new DirectoryInfo(label2.Text);
FileInfo[] Files = dinfo.GetFiles("*.doc");
foreach (FileInfo file in Files)
{
files.Add(file.Name);
listView1.Items.Add(file.Name);
}
dinfo = new DirectoryInfo(path);
Files = dinfo.GetFiles("*.doc");
foreach (FileInfo file in Files)
{
if (files.Contains(file.Name))
{
continue; // We already saw this file
}
listView2.Items.Add(file.Name);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
EDIT
If you want inexact matching, you need to reduce the file name to its essence -- remove any decorations, which in your case looks to be one (or both) of
Leading digits
Underscore followed by whatever
The essence of 01hello_world.doc would thus be hello.
Regex should fit the bill quite nicely -- although the exact definition of the regular expression would depend on your exact requirements.
Define the Regex and a transformation method somewhere suitable:
private static readonly Regex regex = new Regex(
#"[0-9]*(?<core>[^_]+)(_{1}.*)?", RegexOptions.Compiled);
private static string Transform(string fileName)
{
int extension = fileName.LastIndexOf('.');
if (extension >= 0)
{
fileName = fileName.Substring(0, extension);
}
Match match = regex.Match(fileName);
if (match.Success)
{
return match.Groups["core"].Value;
}
return fileName;
}
Then modify the original method to transform the filename before adding files to the HashSet and before checking for their presence:
DirectoryInfo dinfo = new DirectoryInfo(label2.Text);
FileInfo[] Files = dinfo.GetFiles("*.doc");
foreach (FileInfo file in Files)
{
files.Add(Transform(file.Name)); // Here!
listView1.Items.Add(file.Name);
}
dinfo = new DirectoryInfo(path);
Files = dinfo.GetFiles("*.doc");
foreach (FileInfo file in Files)
{
if (files.Contains(Transform(file.Name))) // Here!
{
continue;
}
listView2.Items.Add(file.Name);
}
Note the two calls to Transform.

c# load images from folder

Im trying to load images from a folder and then inserting them into a tag.
For now i have this that i took from another question:
public string GetImage()
{
string imPath;
imPath = HttpContext.Current.Server.MapPath("~/Layout/Images/Banner");
DirectoryInfo directoryInfo = new DirectoryInfo(imPath);
FileInfo[] fileInfo = directoryInfo.GetFiles();
ArrayList arrayList = new ArrayList();
foreach (FileInfo fi in fileInfo)
arrayList.Add(fi.FullName);
return imPath;
}
But its not returning the images, only the folder path.
Just adding an answer so I can point out what everyone is confused about here.
What you've done here:
public string GetImage()
{
string imPath;
imPath = HttpContext.Current.Server.MapPath("~/Layout/Images/Banner");
DirectoryInfo directoryInfo = new DirectoryInfo(imPath);
FileInfo[] fileInfo = directoryInfo.GetFiles();
ArrayList arrayList = new ArrayList();
foreach (FileInfo fi in fileInfo)
arrayList.Add(fi.FullName);
return imPath;
}
Is equivalent to the following:
public string GetImage()
{
return HttpContext.Current.Server.MapPath("~/Layout/Images/Banner");
}
This is why everyone is a little confused.
Perhaps this is what you want (refactored a little bit)?
public ArrayList GetImage()
{
DirectoryInfo directoryInfo = new DirectoryInfo(HttpContext.Current.Server.MapPath("~/Layout/Images/Banner"));
ArrayList arrayList = new ArrayList();
foreach (FileInfo fi in directoryInfo.GetFiles())
arrayList.Add(fi.FullName);
return arrayList;
}
It's because you return the image path.. You probably want the ArrayList you're making.
change:
return imPath;
to:
return arrayList;
and change:
public string GetImage()
to:
public ArrayList GetImage()
I also reccomend renaming that arraylist, and try to do a little more research before asking questions. If you understood what return is, you would've know what the problem is.
And as you can read in the comments of your question, you shouldn't be using ArrayList. I'm no expert on C# so that will have to be answered by someone else.

To get selected list box value with folder path

I have a list box and its populated by this method,
private void ToReadFromExcel_Load(object sender, EventArgs e)
{
string folderpath = #"\\gibson\users";
// Call the method to show available files
PopulateListBox(ExcelListBox, folderpath, "*.csv");
}
// To populate list box with csv files from given path
private void PopulateListBox(ListBox lsb, string Folder, string FileType)
{
DirectoryInfo dinfo = new DirectoryInfo(Folder);
FileInfo[] Files = dinfo.GetFiles(FileType);
foreach (FileInfo file in Files)
{
lsb.Items.Add(file.Name);
}
}
String strItem;
foreach (Object selecteditem in ExcelListBox.SelectedItems)
{
strItem = selecteditem as String;
MessageBox.Show(strItem);
}
// read csv file information and insert into detail table
string filepath = #"\\gibson\users\CampManager.csv";
StreamReader sr = new StreamReader(filepath);
I hard coded the file path now, but I need to pass the filepath that was selected in the listbox. I have the file name in the variable stritem. If I want to pass the whole folder path how would I do that?
There is an ideal way. You should, instead of adding the FileInfo object's Name, should add the FileInfo object itself. So later you will be able to retrieve any piece of info related to that object, in your case say size, parent folder etc, and not just file name. Do it like this:
// To populate list box with csv files from given path
private void PopulateListBox(ListBox lsb, string Folder, string FileType)
{
DirectoryInfo dinfo = new DirectoryInfo(Folder);
FileInfo[] Files = dinfo.GetFiles(FileType);
foreach (FileInfo file in Files)
{
lsb.Items.Add(file); //<-- note here
}
}
String strItem;
foreach (FileInfo selecteditem in ExcelListBox.SelectedItems)
{
StreamReader sr = new StreamReader(selecteditem.FullName);
//or whatever
}
One thing you should take care of here is to set the DisplayMember property of the ListBox like this:
ExcelListBox.DisplayMember = "Name";
What this does is to set the what property of the object in the listbox should be displayed. So here you choose FileInfo.Name which is what you want. This is how typically custom objects are added to ListBoxes in WinForms. You do not add just the string part of it typically. Akin to DisplayMember, there is also ValueMember property which is used to assign a value for each object, probably some id or so, but in your case nothing.
Few suggestions, 1) If you're using .NET 4, then use EnumerateFiles instead of GetFiles. The former is lazy and only yields a result when you start enumerating them (not beforehand), so it should be faster.
foreach (FileInfo file in dinfo.EnumerateFiles(FileType)) //<-- note here
{
lsb.Items.Add(file);
}
2) Use a using clause to dispose your stream reader properly, since that wont lock your file. It's always a good practice to make use of using. Its better on eyes than manual closing and disposing! Like this or so:
foreach (FileInfo selecteditem in ExcelListBox.SelectedItems)
{
using(StreamReader sr = new StreamReader(selecteditem.FullName))
{
//your code here
}
}

listview opening directories

I'm very new to C#. My boss asked me to wrote some code using listview as a file browser. I tried it and it seems it works. This code is to open the files from your drives and display it on the listView. It's very simple. I also made an additional function where one can clear the displayed items in the listView. I would like to add additional feature, where I can also open the directory and not only the files.
By the way, here is the sample of my code:
private void btnOpen_Click(object sender, EventArgs e)
{
string strSelectedPath;
folderBrowserDialog1.ShowDialog();
strSelectedPath = folderBrowserDialog1.SelectedPath;
label1.Text = strSelectedPath;
DirectoryInfo di = new DirectoryInfo(strSelectedPath);
FileInfo[] files = di.GetFiles();
foreach (FileInfo file in files)
{
listView1.Items.Add(file.Name);
}
}
private void btnClear_Click(object sender, EventArgs e)
{
listView1.Items.Clear();
label1.Text="";
}
Could you please show me how?
If I understand your question correctly (you want to list not only the files in the selected directory, but also the sub-directories), you will want to look into the GetDirectories method of the DirectoryInfo class.
You could do something like this, perhaps:
DirectoryInfo di = new DirectoryInfo(strSelectedPath);
// first list sub-directories
DirectoryInfo[] dirs = di.GetDirectories();
foreach (DirectoryInfo dir in dirs)
{
listView1.Items.Add(dir.Name);
}
// then list the files
FileInfo[] files = di.GetFiles();
foreach (FileInfo file in files)
{
listView1.Items.Add(file.Name);
}
Update: I would suggest that you move the above code into a separate method that takes a string parameter for the path (something like ListDirectoryContents(string path)). In this method you can start with clearing the items from the list view, setting the label text and then adding new content as above.
This method can be called from your btnOpen_Click method, passing folderBrowserDialog1.SelectedPath in the path parameter.
I usually try to keep my event handlers as small as possible, preferably not performing any real work but rather just call some other method that does the work. This opens up a bit more for triggering the same functionality from other places.
Say for instance that your application can take a path as a command line parameter, then it will be cleaner code just calling ListDirectoryContents with the path from the command line than perhaps duplicating the same behaviour in your form.
Full code example of that approach:
private void btnOpen_Click(object sender, EventArgs e)
{
string path = BrowseForFolder();
if (path != string.Empty)
{
ListDirectoryContent(path);
}
}
private string BrowseForFolder()
{
FolderBrowserDialog fbd = new FolderBrowserDialog();
if (fbd.ShowDialog() == DialogResult.OK)
{
return fbd.SelectedPath;
}
return string.Empty;
}
private void ListDirectoryContent(string path)
{
label1.Text = path;
listView1.Items.Clear();
DirectoryInfo di = new DirectoryInfo(path);
// first list sub-directories
DirectoryInfo[] dirs = di.GetDirectories();
foreach (DirectoryInfo dir in dirs)
{
listView1.Items.Add(dir.Name);
}
// then list the files
FileInfo[] files = di.GetFiles();
foreach (FileInfo file in files)
{
listView1.Items.Add(file.Name);
}
}
The upsides of this code is that you can reuse the method BrowseForFolder whenever you need to browse for a folder, since it simply returns the selected path and is not connected to what the path will be used for. Similarly, the ListDirectoryContent method is completely unaware of from where the path parameter comes; it may come from the folder browser, it may come from the command line or anywhere else.
If you focus on having your methods performing only their "core task", relying on input parameters for any additional information that they need you will get code that is easier to reuse and maintain.
When it comes to event handlers (such as btnOpen_Click), I like to see them as triggers; they trigger things to happen, but the don't really do a lot themselves.
The DirectoryInfo class contains a GetDirectories method, as well as a GetFiles method. See your original code sample below, modified to add directories:
private void btnOpen_Click(object sender, EventArgs e)
{
string strSelectedPath;
folderBrowserDialog1.ShowDialog();
strSelectedPath = folderBrowserDialog1.SelectedPath;
label1.Text = strSelectedPath;
DirectoryInfo di = new DirectoryInfo(strSelectedPath);
FileInfo[] files = di.GetFiles();
DirectoryInfo[] directories = di.GetDirectories();
foreach (DirectoryInfo directory in directories)
{
listView1.Items.Add("Directory " + directory.Name);
}
foreach (FileInfo file in files)
{
listView1.Items.Add(file.Name);
}
}
I believe this is what you want?
DirectoryInfo[] directories = di.GetDirectories();
foreach (DirectoryInfo directory in directories)
{
listView1.Items.Add(directory.Name);
}

How do I change the Read-only file attribute for each file in a folder using c#?

How do I change the Read-only file attribute for each file in a folder using c#?
Thanks
foreach (string fileName in System.IO.Directory.GetFiles(path))
{
System.IO.FileInfo fileInfo = new System.IO.FileInfo(fileName);
fileInfo.Attributes |= System.IO.FileAttributes.ReadOnly;
// or
fileInfo.IsReadOnly = true;
}
You can try this : iterate on each file and subdirectory :
public void Recurse(DirectoryInfo directory)
{
foreach (FileInfo fi in directory.GetFiles())
{
fi.IsReadOnly = false; // or true
}
foreach (DirectoryInfo subdir in directory.GetDirectories())
{
Recurse(subdir);
}
}
Use File.SetAttributes in a loop iterating over Directory.GetFiles
If you wanted to remove the readonly attributes using pattern matching (e.g. all files in the folder with a .txt extension) you could try something like this:
Directory.EnumerateFiles(path, "*.txt").ToList().ForEach(file => new FileInfo(file).Attributes = FileAttributes.Normal);

Categories

Resources