How can I get the filepath of the current open folder? - c#

I want to be able to have the current open filepath name read and then display the options of filtered extentions. So for example if the currently open folder is say c:\Thisfolder and it has .jpgs and .txt files in it I want to be able to list a single .txt file in a selection area to choose from as the default file. I have tried to use DirectoryInfo and Folder Browser Dialog but no joy at the moment.
Many thanks
I will be sending my clients a folder with jpgs and a single file with an .syvw extention. What I want is for the program to read the .syvw file and show it as an option to open it. In the same way a folder will open showing the contents of a USB or disc once it is inserted into the computer.

Tried:
Using System.IO;
..
String currentPath = Directory.GetCurrentDirectory();
?

Related

c# - Custom Notepad loading in text

So I have made my own custom Notepad program everything works just like a normal notepad but now I want to launch it from .txt files. I change my default launch program for .txt files to my NotePad2.0. The problem is when I i launch the from a .txt file it doesn't load in the text. Anyone got any ideas? I'm looking for a possible file path to the .txt file or some other way that I can pull the text out of the .txt file on open.
(other details: I am talking about when I am in a File Explorer or on the Desktop and I open a .txt file. I want the text that is in that .txt file to load into my program.)
Thank you to anyone who helps/looks over my problem!
Use the Environment.GetCommandLineArgs method to retrieve the filename which is opened and then read all the text inside your file using the File.ReadAllText method and load the resultant string into your app (maybe in a textbox):
textBox1.Text = System.IO.File.ReadAllText(Environment.GetCommanLineArgs()[1]);
Where textBox1 is the text container in your app.
After that, if you open a text file with your app, you will see that all its text is properly loaded into your TextBox.
Note that the first argument is the name of the executable itself. Thus you need the second argument. Also, you might want to check if there is a second argument because otherwise it will crash if you directly open the executable. You might also want additional checks like if the file is a text (.txt) file or not and so on.
For that, you can do something like this:
var args = Environment.GetCommanLineArgs();
if (args.Length > 1)
{
textBox1.Text = System.IO.File.ReadAllText(args[1]);
}

Reading multiple Text files

Just a question, I'am using c# mvc for a project and I have this requirement of getting several text files from a particular path (network path to be precised - eg: \10.0.0.1\SharedFolder). Now, each text files has different text formats and what I have to do is to get any existing files from that path folder and create a single text file for all the text files.
Is it possible to used a network path (the path was specified through user input) using a web application to access files?
If yes, what would you suggest?
I was already looking at the option of using "Browse Folder" button, however, I wasn't getting any progress on how to achieve this.
Thanks in advance.
This is how I would do it =>
Iterate through each file in the directory using the .net Directory class.
For each file:
Use the Textreaderclass to read each line
Use the Textwriter class to write each line to your designated file
Let me know if you still can't figure it out!
Yeah you can,
using (TextWriter writer = File.CreateText("C:\\Output.txt"))
{
//WRITE WHATEVER YOU WANT TO WRITE.
}
Just replace "C:\Output.txt" with the directory you want.
Hope this helps

Limiting the files that can be selected using Open File Dialog box

I have a C# Windows Forms application where I load either a XML file or a CSV file for some task operations. When I click the Browse button I have, an Open File Dialog box appears and I can navigate to a location on my drive and choose the file and then upload it using an Upload button.
If I load a JPG or a ZIP file or any file whose format is anything except CSV or XML, my application crashes. Is there any way of limiting the Open File Dialog box to open only CSV or XML files alone in C#?
Use
openFileDialog.Filter = "CSV files (*.csv)|*.csv|XML files (*.xml)|*.xml";
this way only csv files or xml files are shown.
Odd-numbered pipes delineate between what's visible in the Filter dropdown and the corresponding actual file extension, and Even-numbered pipes delineate between the first entire file extension and the second.
For example, "CSV files (*.csv)|*csv" means users will see "CSV files (*.csv)" in the filter dropdown, and that option will look for any files that match *.csv.
In the line of code above, the pipe before "XML" indicates an entirely new filter option that will appear below the CSV option.
Nevertheless, users can also select other filetypes if they type in the complete name - so check the filename that was selected and correct your code accordingly.
You can use the Filter property to let the user choose a certain type of file.
However! This is not a guarantee. A user is still able to input '(star).(star)' in the filename box and show all files. So you should check the resulting file(s) in your code as well.
You can do this with the Path.GetExtension() method.
You can apply a filter in your Open file dialog which only shows .xml and csv files as mentioned above.
With path.getextension http://msdn.microsoft.com/en-us/library/system.io.path.getextension.aspx You can check if the user indeed selected a file with the right extension. If a wrong extension is selected, you can prompt to select a different file.
I would strongly recommend to check the file extension before upload. Just check the extension after the user had selected the file. If the wrong files was selected, just don't continue the upload/processing...

I want to make Saved File Dialog for a saved file in wpf c#, is it possible?

I have a link label in WPF C#, i want, on clicking that label, it should copy a saved file from specific folder, and save the file to some location wherever user wants to save, just like we do Save As in any software application. In SaveFileDialog class i can see that there is no option to open or copy a saved file and then to save some where else, please let me know how can i do so.
Thanks
Well you can use Open File Dialog and Save File Dialog in sequence in this scenario
First use Open File Dialog to ask user which file to copy
Then use Save File Dialog to save copied file to user defined location
The only pupose of a SaveFileDialog is to let the user choose a path and to get confirmation for overwriting existing files, everything else is your responisbility.

Open HTML file whose relative path is known in a browser using C#

On running my console-based C# application it produces a Results.htm file at a location "../../Template/Output/" and at the end of the program I want to open the Results.htm file in a browser.
Currently, Process.Start("http://www.google.com"); is working but, how do I open a file whose file name and relative path are known?
Should I somehow get the full path of the Results.htm file and use that?
If yes, How do I do that?
Any pointers would help.
you can use the FileInfo with the constructor of the relative file
and then see its attribute of FullName

Categories

Resources