When I want to save a file I use:
FileStream fs = new FileStream(fileName + ".asdf", FileMode.OpenOrCreateOrReadOrBlah);
which will save the file in:
C:\Users\ME\Documents\Visual Studio 2010\Projects\Project A\Project A\Project A\bin\x86\Debug
which is fine
but how do i list the files in that particular folder?
it will be different for every computer
List<string> fileNames = new List<string>();
DirectoryInfo di = new DirectoryInfo(****What goes here?****);
FileInfo[] rgFiles = di.GetFiles("*.asdf");
foreach (FileInfo fi in rgFiles)
{
fileNames.Add(fi.Name);
}
Thanks!
In your save, you are not specifying a directory, so it defaults to the current directory.
var di = new DirectoryInfo(Directory.GetCurrentDirectory());
First off, when you save a file without a path, it goes into the current working directory, not necessarily Debug.
Secondly, you can get the current working directory with:
string currentPath = Environment.CurrentDirectory;
DirectoryInfo di = new DirectoryInfo(****What goes here?****);
"Here" is where the directory name which you want to manipulate would go.
Related
in this I am trying to load all the excel data from specified folderpath and its subfolder present inside it to sql server... Problem is I am able to read the main folder and subfolder, but while taking the data from sub folder file it taking the path of main folder for that sub folder file
example: file path - d:\test
sub directory : d:\test\test2
inside subdirectory my file is present but when iterating it is reading the path of sub directory file as : d:\test\file_name
please help in this
```DirectoryInfo directory = new DirectoryInfo(#FolderPath);
FileInfo[] files = directory.GetFiles("*.xlsx", SearchOption.AllDirectories);```
// Directory structure:
// C:\Temp\ExcelFiles\Excel1.xlsx
// C:\Temp\ExcelFiles\SubFolder\Excel2.xlsx
var path = #"C:\Temp\ExcelFiles";
var allExcelFilesInPathAndSubFolders = Directory.GetFiles(path, "*.xlsx", SearchOption.AllDirectories);
This approach returns the expected 2 Excel file names as string array (i.e. C:\Temp\ExcelFiles\Excel1.xlsx and C:\Temp\ExcelFiles\SubFolder\Excel2.xlsx).
The approach in your post returns an array of FileInfo. You can get all relevant information from that; the name with or without path, the file extension, and using Path.GetFileNameWithoutExtension you can get the name without extension.
// Directory structure:
// C:\Temp\ExcelFiles\Excel1.xlsx
// C:\Temp\ExcelFiles\SubFolder\Excel2.xlsx
var path = #"C:\Temp\ExcelFiles";
DirectoryInfo directory = new DirectoryInfo(path);
FileInfo[] files = directory.GetFiles("*.xlsx", SearchOption.AllDirectories);
// Excel1.xlsx
var firstFileName = files.First().Name;
// Excel1
var firstFileNameWithoutExtension = Path.GetFileNameWithoutExtension(files.First().Name);
// .xlsx
var fileFileExtension = files.First().Extension;
// C:\Temp\ExcelFiles\Excel1.xlsx
var firstFileFullName = files.First().FullName;
My requirement is I have a main folder under which has a set of subfolder. These subfolders are EXPECTED to have a "FileServer.config" file. I need to validate this and Out put a message if the File is missing.
MainFolder--> Contains subfolder 1 ,sub folder 02 , subfolder 03.
So when the user clicks say for example SubFolder01 I want t validate if this file exist in that folder
Currently in the Code I have the code scans for all the folders at once and the output is based on the first folder
string path =# "D:\TEST\PROJ\Repo\";
DirectoryInfo directory = new DirectoryInfo(path);
DirectoryInfo[] subDirectories = directory.GetDirectories();
foreach(DirectoryInfo folder in subDirectories)
{
string subpath = Path.Combine( # "D:\TEST\PROJ\Repo\", folder.Name);
string[] filePaths = Directory.GetFiles(subpath, "fileserver.config");
if(filePaths.Any())
Console.WriteLine(subpath);
}
This will work:
string path = #"D:\TEST\PROJ\Repo\";
DirectoryInfo directory = new DirectoryInfo(path);
foreach (DirectoryInfo folder in directory.GetDirectories())
{
var files = folder.GetFiles("fileserver.config");
if (files.Length > 0)
Console.WriteLine(folder.Name);
}
You said the user clicks on a subdirectory (element), so I assume that you get a concrete folder path. To validate if a certain file is existing in a specific folder try
public bool FileExistsInFolder(string folderPath, string filename)
{
return Directory.Exists(folderPath)
&& Directory.GetFiles(folderPath, fileName).Any();
}
I have a C# console application that sends Excel spreadsheet attachments through email.
I have given the file path in App.config. While trying to find the file, the code looks at proper location. But when trying to attach the file inside the foreach statement, it is looking in code's bin folder.
What am I doing wrong here?
DirectoryInfo dir1 = new DirectoryInfo(ConfigurationManager.AppSettings.Get("FilePath"));
FileInfo[] folderFiles = null;
folderFiles = dir1.GetFiles();
foreach (FileInfo aFile in folderFiles)
{
message.Attachments.Add(new Attachment(aFile.Name));
}
You need to use aFile.FullName (includes the full path) rather than aFile.Name (only the filename). If a command is not doing what you expect, you should check the documentation.
Alternatively, you could make it simpler:
string dir1 = ConfigurationManager.AppSettings.Get("FilePath");
foreach(string aFile in Directory.EnumerateFiles(dir1))
{
message.Attachments.Add(new Attachment(aFile));
}
as Directory.EnumerateFiles simply returns the full filenames and you would have to think about not doing so (e.g. by using Path.GetFileName) to do otherwise.
How can I list the text files in a certain directory (C:\Users\Ece\Documents\Testings) in a listbox of a WinForm(Windows application)?
// What directory are the files in?...
DirectoryInfo dinfo = new DirectoryInfo(#"C:\TestDirectory");
// What type of file do we want?...
FileInfo[] Files = dinfo.GetFiles("*.txt");
// Iterate through each file, displaying only the name inside the listbox...
foreach( FileInfo file in Files )
{
listbox1.Items.Add(file.Name);
}
// A statement, followed by a smiley face...
That oughta do it. ;o)
To get the txt files, try this:
var folder = #"C:\Users\Ece\Documents\Testings";
var txtFiles = Directory.GetFiles(folder, "*.txt");
listBox.Items.AddRange(txtFiles);
Directory.GetFiles(targetDirectory);
Using the above code we get the names(i.e full path) of all the files in the directory. but i need to get only the name of the file and not the path. So how can I get the name of the files alone excluding the path? Or do I need to do the string operations in removing the unwanted part?
EDIT:
TreeNode mNode = new TreeNode(ofd.FileName, 2, 2);
Here ofd is a OpenFileDialog and ofd.FileName is giving the the filename along with it's Path but I need the file name alone.
You could use:
Path.GetFileName(fullPath);
or in your example:
TreeNode mNode = new TreeNode(Path.GetFileName(ofd.FileName), 2, 2);
Use DirectoryInfo and FileInfo if you want to get only the filenames without doing any manual string editing.
DirectoryInfo dir = new DirectoryInfo(dirPath);
foreach (FileInfo file in dir.GetFiles())
Console.WriteLine(file.Name);
Use:
foreach(FileInfo fi in files)
{
Response.Write("fi.Name");
}