Don't judge me, but I have an object with over 100 properties, most of them string.
Is there anyway to automatically add them to the code, without extensive typing?
I have them all in a text file with correct casing. I looked for plug ins, but couldn't find any (maybe using wrong keywords?)
I am assuming your file looks like:
Property
SomeOtherProperty
Test
The easiest way would be to use a CSV -> C# Model generator
Steps
Change it to be comma separated, you can do this with C#:
var path = #"C:\Path\To\Your\File.txt";
var text = File.ReadAllText(path);
text = text.Replace(Environment.NewLine, ",");
File.WriteAllText(path, text);
Open the file and copy its contents to your clipboard.
Now open the C# Class from CSV tool.
Paste the contents and voila you have a C# model!
Notepad++ can do it quite easily.
Open your text file with notepad++ and press Ctrl+H
Fill in the fields like below:
search for (.*)
replace with public string \1 {get;set;}
tick "regular expression"
press "Replace all"
And voilĂ :
Note that this should work with any editor that handle regex (as stated by #Klamsi in the comments section)
Related
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~
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
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
abc #cnn("sujsl d(*) sfv #nor dsf ",dn,".",#tn); ator '`,`') sds
#cns1 or '`,"\','\",`') fdhg #cns2 sf \",dn,"; nj
How can I get this text as a string in C#. It can not be simply done due to containing " I would prefer to paste it, someway, as it is, rather than replcaing " with \" as my text already contains things like that
One way is, to paste it in a static text Box (textBox1) and then get it as Text of textbox (If I have to use this in a winform) e.g. string st = textBox1.Text
How this type of text can be got in a string variable without using a textBox?
This is a good case for resource files. They will allow you to enter string literals without having to worry about escaping of any kind and are available to you programmatically. See for reference http://msdn.microsoft.com/en-us/library/3xhwfctz(v=vs.100).aspx
Check out string literals, this may be what you are looking for.
I am firing up Visual C# (2010 Express) for the first time, and I've created a new project for a WindowsFormsApplication. No code has been written yet, but I created a button and placed it on Form1. Then, I double-clicked the button and am taken to the part of the code where you write what happens when the button is push/clicked.
The first thing I would like to do is read data from a LARGE tab-delimited text file (30MB). The text file contains 7 structured columns of data like names, age, favorite color, animal, etc. Nothing tricky or fancy in the text formatting. I'm using the code below:
File.ReadLines(sourceFilePath)
.Select(line => line.Split('\t'))
.ToArray();
But my more basic question is how do I establish and define File and sourceFilePath? With the code above I get "The name 'File' does not exist in the current context.
You need to add the following line to the top of your C# file:
using System.IO;
This will allow the use of the File class, which is in the System.IO namespace.
As for defining sourceFilePath, that's just a variable, which you can declare and set to whatever file path you need, e.g.
string sourceFilePath = #"c:\data\file.csv";
Note the use of # before the string literal; this prevents the backslashes from being treated as the start of escape sequences. You can instead just escape the bakslashes, e.g.
string sourceFilePath = "c:\\data\\file.csv";
If you want to split by tab key then you can try using ReadAllText method, and then a Split method, where you define delimiter (by tab):
string[] delimitedByTab = File.ReadAllText(#"file").Split('\t').ToArray();
And use System.IO; namespace for File class
The File class resides the System.IO namespace. You leverage it in your application with the following:
using System.IO;
As for defining the contents of sourceFilePath, you are going to need either to hard-code the value to a fixed location, which is not always advisable, or devise a mechanism for the user to specify that path, possibly through one of the various CommonDialogs that are available. Some research on the CommonDialogs should help push you a bit further in developing your project.
I currently have two strings assigned - domain,subdomain
How could I delete any matched occurrences of these strings in a text file?
string domain = "127.0.0.1 test.com"
string subdomain = "127.0.0.1 sub.test.com"
I don't think using a regex would be ideal in this situation.
How can this be done?
You need to:
Open the existing file for input
Open a new file for output
Repeatedly:
Read a line of text from the input
See if it matches your pattern (it's unclear at the moment what pattern you're looking for)
If it doesn't, write the line to the output (or if you're only trying to remove bits of lines, work out which bit you want to write out)
Close both the input and output (a using statement will do this automatically)
Optionally delete the original file and rename the new one if you want to effectively replace the original.
var result = Regex.Replace(File.ReadAllText("file.txt"),
#"127\.0\.0\.1 test\.com|127\.0\.0\.1 sub\.test\.com", string.Empty);
Then write to file obtained result.
So let's say I have a program with just a text box and an okay button. The user types in whatever word he wants, and when he clicks ok, it opens a specific file called Test.doc and CTRL+F for the word "test" and replaces it with whatever the user entered into the text box. How can I open said file and replace instances of the word test with the user's defined word?
Ignoring the format of the document, you could literally use the folowing for any type of file:
var contents = System.IO.File.ReadAllText(#"C:\myDoc.doc");
contents = contents.Replace("Test", "Tested");
System.IO.File.WriteAllText(#"C:\myDoc.doc", contents);
The best way would be to use the ms office interop library though.
Andrew
A number of things:
I'd recommend using a FileDialog to get the file's location. This lets you select the file to edit, but also gives you functionality to only show the file types that you want to handle in this program.
If you're handling .doc's, I'd suggest you look into VSTO and opening word docs. Here's a guide I found after a quick search. I'd suggest using it as a place to start, but you'll need to look around for more specifics.
Lastly, the string.Replace("", ""); method is probably very helpful in the CTRL-F functionality. You should be able to extract a string of the text from whatever document you're analyzing and use that method.