It's a bit hard to explain but I'll try my best. I'm not asking how to create a text file or read text file and display. It's more basic than that.
My question is: I've written a paragraph of text file but I don't know how to put it under Solution Explorer, so my program can reference it instead of writing it many times.
Here is one of my coding with a sample string and I have a couple of them using the same text but different tasks. Here I manually(?) wrote the text(string) that I want to save as text file so I can refer to.
string st = "I like apples. I like red apples. I like red apples than green apples";
string result = st.OrderByDescending(s => s.Split(' ').Count()).First();
this.lblMostWordsInSen.Text = result;
Actually, the code above has an error under Split, it says char doesn't contain a definition for Split. How do I fix it?
I've found this coding below "text_file_name.txt" or (@"d:\\test.txt") is what I want but file should not be stored in my D drive. It should be stored in my program (Solution Explorer?) I did it in Web application but I don't know how to do in WinForm.
string filename = "Alice-in-Wonderland.txt";
StreamReader sr = new StreamReader("TestFile.txt")
String[] values = File.ReadAllText(@"d:\test.csv").Split(',');
And finally how to call my file is my last question...
Thanks in advance~
Instead of using the Resources
of your program as suggested in the comments already, you could also use a Settings
file if you want these Settings to be easily manipulated.
Check this article out: http://msdn.microsoft.com/en-us/library/aa730869(v=vs.80).aspx
Actually, the code above has an error under Split, it says char doesn't contain a definition for Split. How do I fix it?
string result = st.OrderByDescending(s => s.Split(' ').Count()).First();
it is because string st
is a group of characters. in fact it is
st[0],st[1],...,st[st.Length-1]
when you call st.OrderByDescending
and supply a lambda expression like s => ...
, s
does not represent the whole string( st
), it just represents elements of st
which their type is char
and this results to the error we have mentioned above.
You can add a text file to your projects. and if you want to read them you can just read them like this
File.ReadAllText("yourfilename")
but remember to select your file in solution explorer and right click and click properties, then change " Copy to output directory " property to the " Copy always " or " Copy if newer " based on your situation, this will cause that when you build your project, this file will be copied in the directory where your executable file is and you do not need path of it to access it.
you can also go to Build Events tab of your project properties and set actions that will execute when you try to build your project, for example you can set an action to copy "yourfile" to a folder named "text resources" in your build directory, in this approach you can handle more complex situation for example when you have lot of this kind of resources in your project.
you can read this for more information on Build Events
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.