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;
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
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?
How would one list the files in a directory into an Array? Files only, I could care less for folders. I know in python it's:
for file in os.listdir('Blah'):
#BlahBlahBlah
However, I'm not sure how I would go about doing so in C#.
Thank you for your help!
Use Directory.GetFiles method
string[] filesArray = Directory.GetFiles("yourpath");
Returns the names of files (including their paths) in the specified
directory.
Remember to include System.IO
You can also use Directory.GetFiles Method (String, String) to search files by specifying search patterns. Something like:
string[] fileArray = Directory.GetFiles(#"c:\", "X*");
return all files starting with Character X
You may use:
if(Directory.Exists("yourpath"))
to check if the path exists
using System.IO;
string[] files = Directory.GetFiles("PATH");
OR
string[] files = Directory.GetFiles("PATH","*.docx",SearchOption.AllDirectories);
OR
string[] files = Directory.GetFiles("PATH","*.pdf",SearchOption.TopDirectoryOnly);
OR
string[] files = Directory.GetFiles("PATH","*.xlsx");
Try following...Use System.IO directory
string[] filePaths = Directory.GetFiles(#"D:\MyDir\");
I need to list all of the images in a folder using C#.
I searched Stack Overflow and found some threads talking about it, but the questions were covering PHP. I need to do this with C#.
DirectoryInfo di = new DirectoryInfo(#"C:\YourImgDir");
FileInfo[] Images = di.GetFiles("*.jpg");
You can substitute whatever image file extensions you so desire.
This is for C#:
string[] files = Directory.GetFiles("Location of Files", "*.jpg"); //.png, bmp, etc.
You can use Directory.GetFiles to get the filenames of files in a directory:
var files = Directory.GetFiles("directory_path", "*.jpg");
You can change .jpg for any other file type. The asterisk is a wildcard character.
I have a csv file with file names and I need to search for those filenames in a particular directory. I can read thru a csv file and get all the filenames but would like to know how can I search for those files.
Any pointers would be would of great help
What about trying following code snippet.
Directory.EnumerateFiles(directory, fileName, SearchOption.AllDirectories);
System.IO.Directory.Getfiles will give you a list of files in a particular directory. If you need to so more intensive searching. You may want to use the windows indexing.
It's pretty easy, there are many different ways you can do this. I prefer the "exists" method if I am just trying to find out if the files are there.
string SearchDirectory = "C:\\SomeDirectory\\";
List<String> FilesToSearch = new List<string>();
//Populate FilesToSearch from your csv...
foreach (String CurrentFileToSearch in FilesToSearch)
{
if (System.IO.File.Exists(SearchDirectory + TargetFileName))
{
//Do Something!
}
}