Mimic/Create Minimalist File Browser in C# - c#

I'm trying to make a program that stores files as *.txt based documents. I want to be able to click a button and pull up a list of currently stored files
(Located in C:\ProgramData\ProgramName\Incidents)
Above is an example of what I'm trying to accomplish where 140219-000727 is the name of the file, the rest isn't need. Clicking Open or Double Clicking would "Open" that file and parse the .txt into pre-existing forms on a WinForm application that I have already created.
What is the best way to go about doing this with a minimal hit on system resources?

I think Directory.GetFiles is what you are looking for. You can use the simplest mask "*.txt" to fetch all txt files and then using Path.GetFileName cut the file name from the full path.
And later (on double click or button click) use the directory name + file name for opening:
//populating:
var files = Directory.GetFiles(YOUR_FOLDER_PATH, "*.txt");
foreach (var file in files)
{
var fileName = Path.GetFileName(file);
//assuming ListBox:
listBox.Items.Add(filename);
}
//opening (from listbox)
var fileName = Path.Combine(YOUR_FOLDER_PATH, listBox.SelectedItem.ToString());
File.ReadAllText(fileName);

You just need a FolderBrowserDialog control.
var fileNames = new List<string>();
var fileContents = new Dictionary<string, string>();
var filePaths = Directory.EnumerateFiles(folderBrowserDialog1.SelectedPath, "*.txt");
foreach (var filePath in filePaths)
{
var fileName =new FileInfo(filePath).Name;
fileNames.Add(fileName);
fileContents.Add(fileName, File.ReadAllText(filePath));
}

Related

C# not finding correct file path

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 to get and read path filename 4 file on one click c#

i have filename A6010509_DCODE.txt, A6020509_DCODE.txt, A6030509_DCODE.txt, A6040509_DCODE.txt
i want to get allfile and read allfile on one click button
Check with using FileInfo.Name Property
As Example Code
string[] files = Directory.GetFiles(path);
foreach(string file in files)
{
Console.WriteLine(Path.GetFileName(file));
}

Can I copy a image from Internet Explorer's cache loaded by WebBrowser?

I read if I've displayed a image in the webBrowser then I've already downloaded it so I could get it from Internet Explorer tmp files. Is it even possible? if so, any C# code example how to do tgis? I looked at Environment.GetFolderPath(Environment.SpecialFolder.InternetCache) folder but I can't find any image there. Not sure if it's right path. It could save a lot of time sice the image will be downloaded just once.
see c# - Programmatically Copy Files from 'Temporary Internet Files' into other directory - Stack Overflow
below sample code
//see https://stackoverflow.com/questions/19581094/programmatically-copy-files-from-temporary-internet-files-into-other-directory
var path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.InternetCache), "Content.IE5");
string[] files = Directory.GetFiles(path, "*", SearchOption.AllDirectories);
HashSet<string> extensions = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
extensions.Add(".ico");
extensions.Add(".jpg");
extensions.Add(".jpeg");
extensions.Add(".png");
extensions.Add(".gif");
extensions.Add(".bmp");
string DestinationFolder2Copyfiles = #"e:\images\";
HashSet<string> alreadyCopiedFilesHolder = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
foreach (string f in files)
{
string ext = System.IO.Path.GetExtension(f);
if (extensions.Contains(ext))
{
string destFileName = Path.Combine(DestinationFolder2Copyfiles, Path.GetFileName(f));
int i = 0;
while (alreadyCopiedFilesHolder.Contains(destFileName) || System.IO.File.Exists(destFileName))
{
destFileName = Path.Combine(DestinationFolder2Copyfiles, string.Format("{0}_{1}{2}", Path.GetFileNameWithoutExtension(f), i, ext));
i += 1;
}
alreadyCopiedFilesHolder.Add(destFileName);
System.IO.File.Copy(f, destFileName);
}
}

write the list of files in a folder to afile

I wrote a code to read all the files in a folder, then write them to a file. All the code complies and runs okay, but the filenames of the files are not displayed in the new file.
Code:
private void Form1_Load(object sender, EventArgs e)
{
DialogResult result = folderBrowserDialog1.ShowDialog(); // Show the dialog.
// create a list to insert the data into
//put all the files in the root directory into array
string[] array1 = Directory.GetFiles(#"C:\Users\a3708906\Documents\Filereader m 15062012", "*.csv");
// Display all files.
TextWriter tw1 = new StreamWriter("C:/Users/a3708906/Documents/Filereader m 15062012/Filereader m 15062012/listoffiles.txt");
List<string> filenames = new List<string>();
tw1.WriteLine("--- Files: ---");
foreach (string name in array1)
{
tw1.WriteLine(name);
}
tw1.Close();
}
I would be grateful for your assistance.
You took the trouble to ask the user the folder location, yet you don't retrieve that folder location. The code should be
string[] array1 = Directory.GetFiles(folderBrowserDialog1.SelectedPath, "*.csv");
// Display all files.
TextWriter tw1 = new StreamWriter(folderBrowserDialog1.SelectedPath+"/listoffiles.txt");
If the file isn't created (ie its just not there, even if it's just blank) then you problem lies with the stream writer. If this is the case I would suggest changing the direction of slashes so that your path is
TextWriter tw1 = new StreamWriter("C:\\Users\\a370890\\Documents\\Filereader m 15062012\\Filereader m 15062012\\listoffiles.txt");
If the file is created but nothing is written have a look at the flush command.
tw1.Flush();
Set a breakpoint to verify that GetFiles is returning files.
(Consider renaming array1 to something more meaningful)
Set a breakpoint on tw1.WriteLine(name) and ensure it is being hit.
It should be pretty easy to see the problem. My guess is that you simply aren't getting any files returned from GetFiles, but the breakpoints will tell you for sure. If your output file is created but missing the files - this is most likely the case.
If your output file doesn't exist; take a closer look at your file writing code.
I would say that your "space" in your folderpath is messing things up. Try to escape the "whitespace" by following the explanations in the msdn
I think problem is with your file path or file writing capability.
You use folderbrowserdialog but do not use it to get selected file
name. Instead you give path manually. also your output path can have
problem.
Try this :
using(system.IO.StreamWriter tw1 =
new system.IO.StreamWriter(#"C:/Users/a3708906/Documents/Filereader m 15062012/Filereader m 15062012/listoffiles.txt")
{
foreach (string name in array1)
{
tw1.WriteLine(name);
}
}

get full filename

I have a path "C:\Users\Web References"
Under the "Web References" folder, i have *.wsdl file
I want to get the full filename of the *.wsdl file
Thanks!
Something like this:
var files = Directory.GetFiles("C:\\Users\\Web References", "*.wsdl", SearchOption.AllDirectories);
This will return a collection of files - there could be more than one wsdl file in the directory. Take the first:
var wsdlFile = files.FirstOrDefault();
Seeing that no-one is mentioning it: Path.GetFullPath()
First, you have to find it:
var files = Directory.GetFiles(path, "*.wsdl");
and now files will contain full paths to all WSDL files (if any) in path.
// find files by filter
var result = Directory.GetFiles("C:\\Users\\Web References\\", "*.wsdl");
//if you have only one file
return System.IO.Path.GetFileName(result[0]); // "my.wsdl"
The Path class will help you extract just the filename from a file path:
string path = #"C:\Users\Web References";
string[] files = Directory.GetFiles(path, "*.wsdl");
foreach (string filePath in files) {
string filename = Path.GetFileName(filePath); // e.g. myFile.wsdl
}
or using linq
var files = from f in Directory.GetFiles((#"C:\Users\Web References")
where f.EndsWith(".wsdl")
select f;
foreach (var file in files)
...

Categories

Resources