Edit resource files and search string in .cs files. - c#

How can I automate searching for strings in all .cs files and add certain code for localization, where I can use a key in resource files. Let's say there is a
string s = "A"
in cs files. I need to change it to something like,
string s = ("A","ResourceFileKey")
and then add to the resource file keys with country specific values. Is there any tool available? Presently, I am using macros and searching ...

If you just want to get all string literals out of your C# code to put them into your resource file, I suggest not to parse your C# code, but the IL code generated by the C# compiler, that ist much (!) easier.
Here is a helpful link with some code showing how to parse IL code:
http://www.codeproject.com/KB/cs/sdilreader.aspx
That, of course, does not solve your problem how to modify your existing code.

You can write your own. Its a simple String.Replace call.
Read your file using FileStream Execute ReadToEnd method and you'll get a string. Then use String.Replace on it which will again return you a modified string. Replace your file content with the new string and save.

Related

How to convert symbols in file name?

I write a file called "Blaitière.bytes" in C# on OSX. Then I list all files using Directory.GetFiles, I get "Blaitie`re.bytes" instead. Is there a way to convert from one to another? i.e. is there a method for me to know to what my filename will be converted to?
This answer (https://stackoverflow.com/a/6153713/217022) explains why it happens - file names on OSX have to be in fully decomposed unicode, so
calling:
string path = ...my path...
path = path.Normalize(System.Text.NormalizationForm.FormKD);
solves the problem.

Appending Text to a file path

This is probably a simple question, I'm writing a WinForms C# application in VS 2012. I was wondering if there a way to add an extension such as .csv to some writing in a textbox. Say the user wrote in C:\Users\Desktop\filename but left out the .csv part of the path. Is there any way to add the .csv after an execute button is clicked?
Any help would be much appreciated.
You can use Path.ChangeExtension.
// Nota bene: Path.ChangeExtension does not change textBox1.Text directly (or any
// argument given), you MUST use the result if you care about it.
string newPath = Path.ChangeExtension(textBox1.Text, "csv");
The period is optional, and the filename component need not include an extension.
As a future reference, if you can think of something you need to do with a path to a file or a directory...it exists in System.IO.Path. Rare for there not to be support for a common task in that class.
If you do not want to change a valid extension in the string, you could do it like this instead:
// first test for an extension
if(!Path.HasExtension(textBox1.Text.Trim()))
{
// then add on '.csv' if one does not exist
string path = Path.ChangeExtension(textBox1.Text.Trim(), ".csv");
// ... use path ...
}

How to retrieve the full name of a file when passing it a parameter to a C# program

I am writing a utility that edits .docx files. I've made it so that when the user right clicks on the correct type of file, it automatically makes the changes and saves the document with a bit of text appended to the file name. All of this works great, except for the fact that I am receiving heavily truncated file names. If the file name contains more than one word, the string passed to the program is has most of its characters replaced by a single ~. Is there any way to either read the original file name, or have the parameter be the full string?
I found the solution to what I was trying to do. I ended up using the C# method Path.GetFullPath.
string path = Path.GetFullPath(originalpath);
This outputs the full file name as opposed to the truncated one.
http://msdn.microsoft.com/en-us/library/system.io.path.getfullpath.aspx
File.getCanonicalPath will give you what you want
http://msdn.microsoft.com/en-us/library/aa988183(v=vs.80).aspx
Put the whole path between two double quot; like:
var fn = "\"C:\\Path With Spaces And Special Characters\\#\\to\\My File.docx\"";
// send fn as an argument to the other process

Inserting a Line at a specific place in a txt using .net

The process I currently use to insert a string into a text file is to read the file and modify it as I write the file out again which seems to be how everyone is doing it.
Since .net has such a diverse library I was wondering if there was a specific command for inserting a string at a specific line in a txt file.
I would imagine it would look something like this:
dim file as file
file.open("filePath")
file.insert(string, location) 'this isn't actually an option, I tried
No, there's nothing specifically to do that in .NET.
You can do it very simply if you're happy to read all the lines of text in:
var lines = File.ReadAllLines("file.txt").ToList();
lines.Insert(location, text);
File.WriteAllLines("file.txt", lines);
It would be more efficient in terms of memory to write code to copy a line at a time, inserting the new line at the right point while you copy, but that would be more work. If you know your file will be small, I'd go for the above.
You could simply read all the text into a string, then use the string insert method, e.g.
File.WriteAllText("file.txt", File.ReadAllText("file.txt").Insert(startIndex, "value"));

MacVIM Syntax file for CS

I am using MacVim to convert cs files to HTML. The convert function works fine. However, I don't like the default syntax highlighting for cs.
I understand that the cs syntax file in at /Applications/Vim/MacVim.app/Contents/Resources/vim/runtime/syntax/cs.vim, Maintainer by Anduin Withers. Not sure if there is any other way to substitute this one with a better syntax highlight file or update it with a a newer version?
You are probably looking for colorscheme files. Syntax files define which part of text (source code) is what, for example it identifies keywords, function names and variables and so on. Color scheme defines what color should each component get.
To change color try
:colo <name of colorscheme>
To cycle through existing schemes use tab:
:colo <TAB>
You can get new colorschemes (choose the ones you like and save as plain ascii files) and store them in your $HOME/.vim/colors directory which is searched by vim before the standard one that you mentioned above.

Categories