I'm developing a windows phone app. I saved many files on Isolated storage and i need shows them (file names) on ListBox.
My code works fine, but file names is show with extension (filename.doc). I want show file names without extension (filename).
My code:
IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication();
string directory = "./preVenda/*.*";
string[] filenames = myIsolatedStorage.GetFileNames(directory);
List0.Items.Add(filenames);
I tried use "Remove" method, but not work.
You could use Path.GetFileNameWithoutExtension
foreach(string file in filenames)
List0.Items.Add(System.IO.Path.GetFileNameWithoutExtension(file));
Also, you could try without an explicit loop (I suppose that list0 is a System.Windows.Control.ListBox)
list0.ItemsSource = filenames.Select(d => Path.GetFileNameWithoutExtension(d)).ToList();
(Warning. Not able to test this now)
Related
I am using Blazor server. I have paintings in a wwwroot/paintings folder. The painting names are all in the format "XX by YY". I need to generate links to each painting using its name, so the link will also say "XX by YY." I can't know the names ahead of time.
How can I load the paintings into an array, or what is the best way to do this?
This is what you are looking for:
DirectoryInfo d = new DirectoryInfo(#"...\wwwroot\paintings\");
FileInfo[] files = d.GetFiles();
foreach (FileInfo filepath in files)
Console.WriteLine(filepath.Name);
last two lines just to print the names of all files you find. If needed you can specify file format in GetFiles() by putting for example "*.jpg" parameter
Using C#, I want to get the list of files in a folder.
My goal inside a combo box:
File1.txt
File2.txt
File3.txt
Process works when using Console.WriteLine however can't convert the string into the object, see below.
string[] files = Directory.GetFiles(dir);
foreach(string file in files)
ComboBox.Items.AddRange(Path.GetFileName(file));
Help would be appreciated! Thanks in advance!
If you want to stick with your foreach, you just need to change AddRange to Add:
foreach (string filePath in files) comboBox1.Items.Add(Path.GetFileName(filePath));
Here is another way using the LINQ extension method IEnumerable.Select like this:
comboBox1.Items.AddRange(files.Select((string filePath) => Path.GetFileName(filePath)).ToArray());
I am developing a Windows Store application and have about 20 images in the Assets/Backgrounds folder.
How can I get those files from C#?
I only need the file path, for example I need to create a List of strings object with file path. For example the list can be in the format of
List<string> files = new List<string> {"Assets/Backgrounds/file1.jpg",
"Assets/Backgrounds/file2.jpg", ...};
Thanks
First you need To know the Path of your Contents
string ImagesFile = #"Assets/Backgrounds/file1.jpg";
Then you Can Access The file like This:
StorageFolder InstallationFolder = Windows.ApplicationModel.Package.Current.InstalledLocation;
StorageFile file = await InstallationFolder.GetFileAsync(ImagesFile);
This contains all the files in your application package
ResourceManager.Current.MainResourceMap
Use Linq to filter those you need
I'm trying to read files from a directory, but from drive C they are in 8.3 format: ABCDEF~1.EXT. On the other drives it works like a charm.
Some code:
String[] newFiles = Directory.GetFiles(outpath);
label1.Text = newFiles[0];
Any idea? I don't even know how to search for this problem.
Try using DirectoryInfo.GetFiles instead of Directory.GetFiles. This returns FileInfo objects which have more meta data about the files. I don't know that this will have any effect, but it's worth trying.
FileInfo[] newFiles = DirectoryInfo.GetFiles(outpath);
label1.Text = newFiles[0].Name;
I am trying to read all .txt files in a folder using stream reader. I have this now and it works fine for one file but I need to read all files in the folder. This is what I have so far. Any suggestions would be greatly appreciated.
using (var reader = new StreamReader(File.OpenRead(#"C:\ftp\inbox\test.txt")))
You can use Directory.EnumerateFiles() method instead of.
Returns an enumerable collection of file names that match a search
pattern in a specified path.
var txtFiles = Directory.EnumerateFiles(sourceDirectory, "*.txt");
foreach (string currentFile in txtFiles)
{
...
}
You can call Directory.EnumerateFiles() to find all files in a folder.
You can retrieve the files of a directory:
string[] filePaths = Directory.GetFiles(#"c:\MyDir\");
Therefore you can iterate each file performing whatever you want. Ex: reading all lines.
And also you can use a file mask as a second argument for the GetFiles method.
Edit:
Inside this post you can see the difference between EnumerateFiles and GetFiles.
What is the difference between Directory.EnumerateFiles vs Directory.GetFiles?