accessing files in a particular folder in a window application - c#

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.

Related

How can I save multiple files in one, just like in a ZipFile?

If I want to export my things i did inside of my app, I need to save two pictures and one text file. This is obviously annoying for the user, as he needs to move three files around.
If I put pictures into a word file and save it as a .docx, I can open it as a ZipFile and see that it actually just contains a lot of xml files and the pictures I added.
I want my app to do the same, create a .myappextension file that contains the other 3 files. But I have no idea how to achieve this. My output would be one string and two images, which I can all save on their own already. Before actually saving it I have an IRandomAccessStream.
In short, can I compile three IRandomAccessStreams into one file?
Just create a zip file, as long as System.IO.Compression namespace's classes are available for uwp.
System.IO namespaces for UWP apps

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

Finding full directory path

My C# application downloads a .zip that contains at least a .dcm file.
After decompression I get something like:
download/XXXX/YYYYYY/ZZZZZZ/file.dcm
I don't know the name of these intermediary X,Y,Z folders, and I don't know how many of them exist, but I'm certain that at least a single .dcm file exists at the end of the path.
How can I get the full path of folders between download and the folder with .dcm files? (assume Windows filesystem and .Net Framework 4.0).
This will give you a list of all the files contained within the download file that would match your filename:
Directory.GetFiles("C:\\path_to_download_folder", "file.dcm", SearchOption.AllDirectories);
You could then parse the returned filepaths for whatever parts you needed. The System.IO.Path methods will probably give you want you need instead of rolling your own.
Additionally, if your application might be downloading multiple files throughout the day, and you always need to retrieve the path of the very latest matching file, you could send the filepath to a System.IO.FileInfo, which lets you get the creation time of the file, which you could use to determine which file is the newest.

load excel template file and write data to cell?

I have a excel template file (C:\Report\Template\abc.xls).
I need to write a C# console application to do following,
copy the abc.xls file from Template folder and save the same template with different name and different folder "Data" ((C:\Report\Data\new_abc.xls)
load the "new_abc.xls" file into memory and write data (comes from database) to it's specific cell (for example i want to write in cell H17)
Please let me suggest or give me link or code to do this. Thanks!
Copying files is easily done with File.Copy. Simply pass in the source path from which to copy and the destination path to copy to as strings.
Take a look at the Microsoft.Office.Interop.Excel classes for manipulating Excel workbooks. Here's a good code sample to get you started. Also this is another overview.

Include editable file in c# .exe

I would like to embed a .txt file into my C# project storing a list values for the user. These values should be configurable and therefore the .txt will have to be edited during runtime. I have found out that Embedded Resources cannot be modified. Is there any other way to do that?
Thank you.
Store the text file as an embedded resource. The first time your program is run, copy the embedded resource to a file on disk, and use it for the configuration. Your users can edit the disk file.
The embedded resource version serves as a default configuration.
You can use your app.config or web.config configuration files.
Normall if you will use large amout of data using a database is recommended. I assume you really need just a .txt document. In your assembly write a procedure that will create that text file if its not present. To be more specific lets say your program is mainProgram.exe. In onload event of mainprogram.exe write a procedure checkTxtFile(). This procedure first will check if there is a txt file in the directory.If the file is not presend it will create it with the desired values.
You can create a XML file using Filestream and using it during runtime or
make your own protocol format and store the data in the file so that no one can change it.
Over to that you can also encrypt and decrypt the file on the fly.
Lists of modifyable, persistable values are best stored in a database. There are many lightweight options to choose from; in this case I think a SqlLite database would suit your purposes best.

Categories

Resources