Regarding StreamReader - c#

Why is this not working ?
StreamReader m = new StreamReader("../folder1/email.html", System.Text.Encoding.UTF8);
code file and html file are in diff folders so I that its some path issue but its not because I just now copied this html file in the same folder where this code file is and changed code to:
StreamReader m = new StreamReader("email.html", System.Text.Encoding.UTF8);
still not working.. What's wrong? Is the syntax wrong or what?

If you use a relative path it will be relative to the bin/Debug or bin/Release folder, not the project folder where your code file is, so try:
m= new StreamReader("../../email.html", System.Text.Encoding.UTF8);

You say that there's no exception with your code. This means that the file is successfully opened for reading. I suspect that you are not reading anything from this StreamReader, you are simply instantiating it and probably not releasing.
Make sure you dispose this stream or you might leak handles. If all you need to do is read the file contents you could use the ReadAllText method:
string contents = File.ReadAllText("email.html");
If the file is not found you will get an exception.

You are probably not reading it ...try this ...put the file in your Bin/Debug directory and...
StreamReader m = new StreamReader("email.html", System.Text.Encoding.UTF8);
Console.Write(m.ReadToEnd());
Console.ReadLine();

Related

Getting the contents of a file in Visual Studio without opening the file from an extension

I'm trying to read the contents of a file in a Visual Studio extension. The following code works, but forces me to open the file, if it isn't (otherwise it crashes):
textDocument = (TextDocument)projectItem.Document.Object("TextDocument");
EditPoint editPoint = textDocument.StartPoint.CreateEditPoint();
string text = editPoint.GetText(textDocument.EndPoint);
I can get the path of the project, so I suppose I could make an educated guess as to the location of the project item. However, ideally I'd like to either get the file contents without opening it; or, alternatively, get the path to the project item (then I could just use System.IO to access the file contents).
I've looked, but don't seem to be able to find any mention of either of these. Can anyone point me in the right direction, please?
You can get the path from a ProjectItem by reading its properties.
var path = YourProjectItem.Properties.Item("FullPath").Value.ToString()
After you have the path you can read its content with System.IO.
string content = File.ReadAllText(path);
If the file is somewhat larger and you are getting troubles with the current code due to size, you should take a look at the StreamReader class.
I'm not sure if this is possible for extensions but you could probably use System.IO, like this:
using System.IO;
string filePath = #"C:\Whatever\YourFileName.txt";
string fileText = File.ReadAllText(filePath);
You could also use StreamReader like this:
using System.IO;
string filePath = #"C:\Whatever\YourFileName.txt";
using (StreamReader sr = new StreamReader(filePath))
fileText = sr.ReadToEnd();
EDIT:
I think I understand you better now.
The only way to "get the file contents without opening it" would be if the extension were to give you that data actively, but I can safely assume it doesn't.
When reading a file, you should already know where the file is (if you don't know then either you're not intended to access that file or you just haven't looked long enough).
I'd try searching the SDK files manually (Or with a file crawler).

asp.net web form C# Streamreader can't find the text file

Trying to make a silly web form that keeps some small information in a text file inside a "data" folder. The code is:
StreamReader sr = new StreamReader(Server.MapPath("data/FeatureList.txt"));
String FileText = sr.ReadToEnd().ToString();
sr.Close();
Getting an exception:
Could not find file 'C:\Users\Tom\Documents\Visual Studio 2013\WebSites\WebSite2\data\FeatureList.txt'.
And, naturally, the file is there right in the specified folder.
Perhaps I have a permission error and IIS can't read the folder or file? Maybe I need to tell Visual Studio 2013 something about this folder? I haven't played with one of these asp.net programs in a while, not since... Well, a couple years ago I made a web page that reads text files and adds captions to displayed photos.
You may use the Path.Combine , it is a good method to get a valid path
StreamReader sr = new StreamReader(Path.Combine(Server.MapPath("~"), #"data/FeatureList.txt"));
String FileText = sr.ReadToEnd().ToString();
sr.Close();
Try,
string path = (string.Format("{0}\\{1}", AppDomain.CurrentDomain.BaseDirectory,"\data\FeatureList.txt));

I get FileNotFound exception when StreamReader tryes to read a txt file [duplicate]

This question already has an answer here:
C# I'm getting a FileNotFound Exception when using StreamReader to read a text file inside resources
(1 answer)
Closed 10 years ago.
using (StreamReader sr = new StreamReader("gold.txt"))
{
text.Text = sr.ReadToEnd();
}
Why I'm getting this error? I simply created the text file and tryed to make a new StreamReader to read gold.txt.
If you have "gold.txt" in your project in visual studio, make sure that the property "Copy to Output Directory" is set to "Copy if newer" or "Copy always".
Hope this helps. I have failed on this point many, many times :P
Also, it is always a good idea to check if File.Exists("gold.txt") before reading. I also tend to use File.OpenText or similar, as I feel it make sit more obvious that you are working with a file.
Remember to Close();
When you give StreamReader a non-qualified path as a parameter it will look for the file in the application's working directory. If the file isn't located there, you'd probably want to give it a fully qualified path, for example:
StreamReader sr = new StreamReader(#"C:\Path\To\gold.txt")
The reason you are getting this error is because the system cannot find the file based on the path you have passed to the StreamReader. This is probably because the file is not in the directory that the application is looking by default.
As already suggested using the full path, or putting the file in the right location should solve the problem in the short term. However, at some point you'll probably want to ask the user to locate the file like this:
OpenFileDialog openFileDialog = new OpenFileDialog();
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
string filePath = openFileDialog.FileName;
using (StreamReader sr = new StreamReader(filePath))
{
text.Text = sr.ReadToEnd();
}
}

How can I open a text file relative to my MVC project?

In my bin file I have set up som test data, and I want my application to be able to access logfiles that are stored in bin/log/log00001.txt.
However, in my crontroller, when I try to use a TextReader on the following path it goes somewhere else: new StreamReader("log/log00001.txt")
How do I read stuff relative to my project?
Try using
StreamReader reader = new StreamReader(Server.MapPath("~/bin/log/log00001.txt"));
HttpContext.Current.Server.MapPath("~/some/path/relative/to/your/web/app")

Path to read file from inside www folder

I have a binary reader to read a file
BinaryWriter bw2 = new BinaryWriter(File.Open(#"c:\test\test6.xml", FileMode.OpenOrCreate));
the path i have set to is c:\test\test.xml
However it needs to read the file from www folder hosted site
so www\test\test.xml
should it be ~\test\test.xml?
Not sure.
Thanks for your help
Check out Server.MapPath() http://msdn.microsoft.com/en-us/library/ms524632(v=VS.90).aspx
So in your case, you're after this:
using (BinaryWriter bw2 = new BinaryWriter(File.Open(Server.MapPath(#"~\test\test6.xml", FileMode.OpenOrCreate)))
{
...
}
Notice I added the using() which is a best-practice for working with expensive resources like files.
Of course, you really should seperate out file opening from object creation so you can have better diagnostics in your code.
Probably you need this function: http://msdn.microsoft.com/en-us/library/ms524632(v=vs.90).aspx

Categories

Resources