My listBox1 is filled with names of txt files. It is sorting them automatically alphabetically. But I need to sort these files by creation time, from the newest to the oldest. Can someone help me?
you are not giving us much to go on :P
But I am going to give it my best shot to help you. Because I do not know how you have populated the List (by hand, or with code) I cant really guide you there.
But lets say you did it by code. The following snippet I typed might help you find what you need
//array of file names (these need to be paths to files, so if you do not prefix it with ./path/ or anything, then the executable needs to be in the same folder)
//If you are debugging, make sure the paths are correct if you expect them to be in the same folder, it should then be in the debug folder.
//If you included some test files in the solution and want them copied to your debug folder. Click on them, go to the properties pane, and set build action to content, and one of the other properties should read copy of newer instead of never copy
var fileNames = new string[]{"1.txt", "2.txt", "3.txt"};
var fileInfos = fileNames.Select(f => new FileInfo(f));
var orderedFileInfos = fileInfos.OrderBy(f => f.CreationTime);
orderedFileInfos is now a list of type FileInfo ordered by ascending on their creationtime.
This is what you can use to populate your ListBox
I hope i helped you progress in your project.
Related
I am developing Windows Form application and I want to have a list of .xlsx files in the folder say -
C:\Equipment\Exchanger\AES\
The folder may contain files like -
00-E-001,
00-E-002,
03-E-005,
04-E-001 and so on..
I don't want to open file dialogs but want to see a combo box showing the FileName (no extension). Based on selection of combobox I want to show the values of certain cells in the various text boxes but that is later part.
You can use Directory.GetFiles to query the full path strings of every *.xlsx file in the folder, then strip it down to just the filename using Select and Path.GetFileName.. It makes sense (to me) to sort them alphabetically, then you just need them in a list so the combo can use them
yourCombo.DataSource = Directory.GetFiles(yourFolderPath, "*.xlsx").Select(Path.GetFileName).OrderBy(n => n.ToLower()).ToList();
If you don't want the extension in the combo (it's a waste of pixels :) ) you can use Path.GetFileNameWithoutExtension
When the time comes to get which file was selected by the user it's:
var fullPath = Path.Combine(yourFolderPath, yourCombo.SelectedItem);
(You'll have to add the extension back on if you used GetFilenameWithoutExtension: yourCombo.SelectedItem + ".xlsx")
I recommend you set your combo's DropDownStyle to DropDownList
First of all, your code should have a look at the folder, which contains needed files.
List<string> xlsxFiles = Directory.GetFiles(yourdirectory, "*.*", SearchOption.AllDirectories)
.Where(file => new string[] { ".xlsx" }
.Contains(Path.GetExtension(file)))
.ToList(); // Looking into directory and filtering files
for(int i = 0; i < xlsxFiles.Count; i++)
{
xlsxFiles[i] = Path.GetFileName(Path.ChangeExtension(xlsxFiles[i], null)); // Removes files' extensions
}
This is the simple way of looking into the directory and filtering files to needed ones.
Then, I suggest having a look into EPPlus library, which allows you to work directly with tables and cells, without manual extraction of the file
Here is the handy tutorial of using EPPlus
I want to display a list of recently modified items, sorted by date. They are contained in folders as there are multiple types of information I need to store for each item. All items are stored in the localfolder.
One way would be to rename the folders by their date when the item is modified, but this could create collisions if two items are modified at the same time. Is there a way to sort the folders by the last modified time that Explorer shows?
The DirectoryInfo objects GetDirectories() method returns an array of DirectoryInfo objects with the various dates populated -- including LastWriteTime.
Just sort by the appropriate property and do what you want with the result.
For reference: https://learn.microsoft.com/en-us/dotnet/api/system.io.directoryinfo.getdirectories?view=netframework-4.7.1
If you are wanting the last modified date for the individual files within a folder, GetFiles()' method returns the an array ofFileInfo` objects, again with various date properties populated.
For reference: https://learn.microsoft.com/en-us/dotnet/api/system.io.directoryinfo.getfiles?view=netframework-4.7.1
As an alternative, if the number of files/folders is large or has to be read frequently, it may be better to keep a text files in each folder and update it with each change to contain a summary of the current state of the files in that folder.
So what I am trying to do is save the contents from a listBox to the application Properties.Settings.Default
I have no idea where to start, nor do I know if it is even possible. Thanks in advance.
any settings you have setup in Properties-> settings tab should show up like
[your namespace].Properties.Settings.Default.yoursetting = "change";
after you edit your properties always call
[your namespace].Properties.Settings.Default.Save();
the save part got me at first.
for list types use :
it will help if your objects can be converted to and from strings anything more not really sure for anything more complex but I hope this gets you started.
foreach(string s in listbox.Items){[settingscode].add(s);}
something like that anyways.
Is is possible to get files that is ordered same as in Windows Explorer
I know "natural sort", but it's not what I need, I need to get the file list ordered by the same attribute in Windows Explorer, for example:
If I ordered a directory by the attribute "create date", then I will get a file list as below:
name create date file size
1.txt 2012/1/1 125Kb
2.tab 2012/3/2 15Kb
3.bmp 2013/5/5 26Kb
If my windows explorer order file list with the attribute "file size", the the file list would be:
name create date file size
2.tab 2012/3/2 15Kb
3.bmp 2013/5/5 26Kb
1.txt 2012/1/1 125Kb
Could anyone help?
I think this is going to be a lot more complex than you expect. Folder settings are stored in the registry in two places:
HKCU\Software\Microsoft\Windows\Shell\BagMRU
HKCU\Software\Microsoft\Windows\Shell\Bags
The first path contains a structure which reflects the structure of the file system, and the second path contains details about those items, including a REG_BINARY value called "Sort" which records the sort order used for that folder.
See Willi Balenthin's website for details on the structure, including sample code (in Python)
Here's how to get a list of files sorted by their name:
var path = #"C:\windows"; // obviously change this to whatever you want
var files = System.IO.Directory.GetFiles (path).ToList ();
file.Sort();
And that's it!
Here's how you would do it per your given code sample:
var temperaturePressureSignalFilesList = Directory.GetFiles(TemperaturePressureSignalDirectory, "*.txt", SearchOption.TopDirectoryOnly).ToList();
temperaturePressureSignalFilesList.Sort();
using System.Linq;
DirectoryInfo info = new DirectoryInfo(""); FileInfo[] files =
info.GetFiles().OrderBy(p => p.CreationTime).ToArray(); foreach
(FileInfo file in files) {
// DO Something... }
here is the sample code for get files in directory by creation time.
You can get files by size same way.
I guess you are talking about viewing pane in Windows Explorer (it's essentially a Windows File Manager but also known under different name). Some clarification is needed. You can apply your custom sorting on various columns; moreover, you can have multiple viewing panes (windows) open sorted on different columns. Thus, the problem definition is a bit unclear.
Assuming that you know the sorting order in your viewing panes, then you can use System.IO.DirectoryInfo and derived FileSystemInfo[] objects; the latter has files.OrderBy method.
Hope this will help. My best, Alex
If you want natural sort order, you should either P/Invoke StrCmpLogicalW (http://msdn.microsoft.com/en-us/library/bb759947.aspx) or find a managed natural sort algorithm. There is no built-in natural sort in .NET Framework.
I think you cannot know which is the order in the pane (by size, name or whatever), you must read the list and then sort it the way you want or prompt the user to select a sorting attribute.
As Kenny posted Sorting Directory.GetFiles() here is an approach, anyway I still thinking there is no possibly way to know which is the sorting order that user selected in the viewing pane.
I think you would have to write a shell extension for windows explorer that captures sort events on columns and writes that metadata to disk in some structured way. You may have multiple explorer windows open so might be an idea to apply timestamp or id so you know which explorer window you are dealing with. Then in your app read that metadata to get the sort order and apply accordingly. Not easy but doable.
Given a particular path of folder in tfs, I need to recursively find all files and folders within the folder for a given changeset. In other words, i need to get the transitive closure of a path in tfs for a given changeset. The problem I'm facing in doing so is listing the contents of a particular folder within tfs..
How would this be possible in C# ?
I'm assuming you want 'folder contents as of changeset X' and not 'folder contents that were part of changeset X'
GetItems is the right call to use, just pass in a version spec for the changeset you're interested in.
http://msdn.microsoft.com/en-US/library/bb138911.aspx
so, assuming you already have a reference to the VersionControlServer instance:
var myFolderAtChangeset17 = versionControlServer.GetItems("$/MyFolder", new ChangesetVersionSpec(17), RecursionType.Full);
If I misunderstood and you happen to want to 'folder contents that were part of changeset X', there's a few different ways of doing it, but getting the changeset with GetChangeset and just filtering the Changes is pretty simple.
Something like this might be more what you're looking for. This gets all changes in a changeset and iterates through them, identifying the ones in the given path. This could be shortened with a linq query, but I'm leaving it a bit more expanded to give the gist of what I'm trying to say:
TeamFoundationServer tfs = new TeamFoundationServer("http://tfs:8080");
VersionControlServer vcs = tfs.GetService<VersionControlServer>();
Changeset cs = vcs.GetChangeset(6284868);
foreach (Change change in cs.Changes)
{
if (change.Item.ServerItem.StartsWith("$/Application Common/Dev/src"))
{
System.Diagnostics.Debug.WriteLine(string.Format("Changeset {0}, file {1}, changes {2}",
cs.ChangesetId, change.Item.ServerItem, change.ChangeType.ToString()));
}
}
I think something like this would work..
TeamFoundationServer tfs = new TeamFoundationServer("http://tfs:8080");
VersionControlServer vcs = tfs.GetService();
ItemSet items;
items = vcs.GetItems(tfsPath, RecursionType.Full);
If you have any other ideas, please post them..
You can use the changeset webservice to get an XML document that contains all of the changed items for a particular changeset. Then just loop through the list of changed items and see if they are in the path you are looking for.
Here's the URL to the changeset webservice:
http://your_tfs_server/VersionControl/Changeset.aspx?artifactMoniker=your_changeset_number&webView=true