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();
Related
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
We have a very old file delivery application(IPGear, if you have heard about it, written in tcl). We upload our IP files there and our customers download it from the system.
When you upload a file to this application, it adds .RCA extension to uploaded file and add some metadata to file. if we view the content of any file in a text editor(Usually tgz, pdf and text files), we see some metadata added to the top of the file by the application(5-10 lines, readable).
If you download a file from the system, they somehow strip this metadata from the file and returns as TGZ file which works fine(we can extract it)
if we find that RCA file on the storage where this application keeps files and edit the metadata they have added via text editor, we are able to extract the file without any problem., which fine too. But we need to do this process for 22K files, therefore we need to script it.
We are able to find the bits the application adds by opening via StreamReader, and strip the metadata and write file to the disk via StreamWriter. However, the file we write to the system is corrupted if it is TGZ file. if we do same thing for text files, they work.
the content of the tgz file looks below when we open in text editor
The bits on lines 29-38 are the metadata we strip.
it looks like the streamreader is not able to write this content back to disk even if we tried different encoding settings.
One another note about this is that the file we are trying to read and write is copied from a Solaris based server into local machine(Windows 7) via WinSCP.
So, my question is, what is the best way of reading TGZ file into memory(as text) so manipulation, and save back without corruption? is streamreader and streamwriter not good for this purpose?
I tried to give as much information as I can, please add comments if you need more clarification.
it looks like the streamreader is not able to write this content back to disk even if we tried different encoding settings.
Yes, because a tgz file isn't plain text. StreamReader and StreamWriter are for text content, not arbitrary binary content.
So, my question is, what is the best way of reading TGZ file into memory(as text)
You don't. You read it as binary data, because it is binary data.
If the TGZ archive contains text files, you'll need to decompress the TGZ to the TAR format, then extract the relevant data from that. Then you can work with it as text. Before that point, it's just binary data.
But it sounds like you actually may just want to read text information before the TGZ file... in which case you need to work out where that text information ends, and not read any of the TGZ file as text (because it's not). This is non-trivial, but if you know that the text is in ASCII it'll be a bit easier - you will need to work out how to detect the end of the text and the start of the real content though, and we can't really tell that from the screenshot you've given.
Ive been trying for a while now to get a rich textbox to populate from a text file. Normally I would say this is easy and there are tons of solutions on here for it. My only issue is I don't want the open file dialog to populate it I want it to be populated on the onload event of the form, and populate from a known text file on the network drive here. Every solution I find seems to be asking for the users input to open the file I don't want this. This is due to the fact that when the text file is edited( which it will often be) I want the textbox to mimic this rather then have an outdated version hard coded in. Please help
If you know the location then
const string Myfile = #"c:\test\myfile.rtf";
richTextBox1.Loadfile(Myfile);
you can use UNC etc, but it may error if it cant find the file - so I would suggest file checks etc.
Use System.IO.FileSystemWatcher to watch for changes to the file, and then System.IO.File.ReadAllText to read the contents of the file into your textbox.
i have a project using asp.net mvc 3 C#. and now i want to have a view that can read word file. i want it looks like SkyDrive, just select the word file (.docx) and then it will show the file in my view. i don't have any idea how to start, can you explain me how to make it happen ?
thanks a lot.
Use Open Xml SDK. It's free, you don't need any Office libraries and there are many resources on the web. This one, for example.
If you just want to show an image of the document then you can open the .docx file as a zip and extract the docProps\thumbnail.wmf file.
i am using a iframe in my web page. I want to open a file c:\Dir\SubDir\xyz.doc inside a iframe.How to do this ? I checked out many sources ,all of them specified to give the source of the iframe to the path by adding the code.Its not working for me. Here is my code
iframe1.attributes["src"] ="c:\\Dir\SubDir\xyz.doc"
assuming you want to open a file on the client's computer, you would need to use a file URL, such as "file://c|/Dir/SubDir/xyz.doc". that said, it still may not work correctly for various reasons (probably primarily security-related).
if my assumption is wrong and you want to display a file on the server, you still need to use a URL and not a file path, meaning you'd have to have the file somewhere under the document root: "../Dir/SubDir/xyz.doc" for example.
then there is the strong likelihood that a .doc file will not render correctly on a webpage.
<iframe src="test.doc"></iframe>
above example is working in my environment, cause I put file inside my project folder and access that file from there..
you can not open any file out side of your project folder.... So please keep this in mind.
#jcomeau_ictx is right you cannot open .doc file in browser but yes it will ask you to save file, if you put in iframe..