Reading multiple Text files - c#

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

Related

How can I get the filepath of the current open folder?

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();
?

How to get the current file path and know if the file is saved

I'm making a simple text editor in .net. I need to know how to get two things to work:
I need to know the full path of the current working file in the editor
I need to prompt the user to save the file on exit if it's not already saved (at all)
Please explain on how to get the above implemented. Thanks! :)
Look into the OpenFileDialog class which will explain how you can select file from your file system. This is where you can start.
Saving the file is simple, you can check the file exists by using System.IO; for example the File and FileInfo classes to start with, then come back here for further questions, which will be inevitable.
Good luck.

Append and Clear Text File Web Form C#

There is an existing process that is writing to a text file on my site. Let's say the file is at http://www.mysite.com/addresses/addresses.txt
I have been able to successfully display the contents of the file in a text box using client.DownloadString, however I can't find a clear answer on how to append to the end of this text file and also to clear the contents of this file. This is a text file on my web server.
I'm working in C#. I do know the path to the file on the server at c:\inetpub\site\addresses\address.txt as well.
Any ideas on how to append or clear this file? Everything I seem to find on it is on windows forms and i don't use enough DotNet to know where to look.
Thanks!
Check out this Article CodeSnip: Read and Write Text Files with ASP.NET 2.0
System.IO.StreamWriter StreamWriter1 =
new System.IO.StreamWriter(Server.MapPath("test.txt"));
StreamWriter1.WriteLine(TextBox1.Text);
StreamWriter1.Close();

Extract and open PPT from resources in C#

I want to view presentation in PowerPoint viewer, ppt file is in a resources. so the problem is that how can i access it and view in PowerPoint viewer.
Here is sample code
Process.Start(#"C:\Program Files\Microsoft Office\Office12\PPTVIEW.exe",**#"e:\presentation.ppt")**;
How can i replace this path by ppt containing in resources?
Actually, what you ask for is a common pattern and there are some related questions and answers here on SO.
Basically what you do in general is the following:
locate the resource in question and open a resource stream to it.
Save the stream to a (temporary) file if your target API cannot deal with streams or byte arrays directly.
Perform whatever operation on the file or directly on the stream/byte array (as I said, if supported).
Eventually remove the temporary file, if any, from step 1.
So, you first extract the PPT file (actually it doesn't really matter that it is a PPT file, could by any file or byte blob for that matter).
string tempFile = Path.GetTempFileName();
using (Stream input = assembly.GetManifestResourceStream("MyPresentation.PPT"))
using (Stream output = File.Create(tempFile))
{
input.CopyTo(output); // Stream.CopyTo() is new in .NET 4.0, used for simplicity and illustration purposes.
}
Then you open it using Process.Start(). You don't need to specify the path to the Powerpoint executable, as PPT should be a registered file extension with either PowerPoint or the PowerPoint Viewer. If you have both installed, you may still want to provide the path to the relevant executable to prevent launching the wrong application. Make sure that you don't hardcode the path though, but try to retrieve it from the registry (or similar, I haven't checked because that gets too specific now).
using (var process = Process.Start(tempFile))
{
process.WaitForExit();
// remove temporary file after use
File.Delete(tempFile);
}
Note: I left out quite some error handling that you might want to add in a real application.

accessing files in a particular folder in a window application

I want to select multiple files based on their specific contents from a particular folder and edit their contents. I am using winform in C#.
any idea which are the classes that can be used.
It would be helpful if code given for example.
thanks.
have a look at Directory.GetFiles to get to the names of the files in a directory. If they're text files you can read them via File.ReadAllLines (returnsan array of strings, one per line of the file) or File.ReadAllText (returns a single string containing the entire content of the file).
To save the edited files have a look at File.WriteAllLines or File.WriteAllText.

Categories

Resources