How to find/replace several strings in VS 2012 Solution - c#

I need to replace about 2000 strings with another string, i tried writting a VS Add-In to iterate through a list of strings and to replace one by one in all files, somthing like this:
foreah(item in strings)
{
findWin.FindWhat = item;
findWin.ReplaceWith = "string2";
findWin.Action = vsFindAction.vsFindActionReplaceAll;
findWin.Target = vsFindTarget.vsFindTargetFiles;
findWin.KeepModifiedDocumentsOpen = true;
findWin.WaitForFindToComplete = true;
findWin.Execute();
}
However, it only works if i set the find options to OpenDocuments, otherwise it throws an AccessViolation exception. There are about 8 projects in the solution and a lot files in there.
Is there any clean/better way to achieve that?
Thank you

You can try the DTE.Find.FindReplace method which is better suited when you don't need to interact with the Find/Replace dialog.

Related

If else formatting Visual Studio not indenting correctly

So this is going to be a kind of strange question, I have never run into this formatting issue with any other programs I have written. I think it is because my if statements has multiple || which needed multiple lines to make it appear more readable.
Notice the dotted line under my else if(). It is getting shifted right for some reason under the if instead of the else.
Is there some kind of formatting setting that can fix this? Or is there a different way I am supposed to do this else if statement? Having all those OR checks is pretty much necessary but I did not wan't to put them all on a single line as that would look real ugly.
Thanks!
Tools -> Options -> Text Editor -> C# -> Code Style -> Formatting -> New Lines
Uncheck everything and save.
Then, CTRL + E, D to format the document.
Also, you're using a huge compound if statement there, which is quite frankly a mess. If you need to check multiple items as starting values, put them all into a list.
List<String> ModelNumberPrefixes = new List<String>();
ModelNumberPrefixes.Add("A1C1C");
ModelNumberPrefixes.Add("A1C1D");
//etc
ModelNumberPrefixes.ForEach(s => {
if (ModelNumber.StartsWith(s)) {
//Whatever you need to do in your big if block
}
});
To format a selection: Ctrl+K, Ctrl+F
To format a document: Ctrl+K, Ctrl+D
See the pre-defined keyboard shortcuts . (These two are Edit.FormatSelection and Edit.FormatDocument.)

C# Localization Shortcuts

I don´t know how i should explain what i want to do.
In my Project are basicly 3 Sub-Projects.
First Project: Holds all Properties and Localizations.
Second Project is an Console Project, using the Properties from the First Project and some Localized Strings.
Third Project is an Windows Forms Project, using all Properties and Localized Strings from the First Project.
The Localization is working without trouble, i can start both Projects and when i change the Culture, it changes the Strings.
But: I have to Write the whole Path to the String everywhere.
Example:
Label.Text = Config.Localization.MyString
With the Settings, i can use an "Shortcut" by setting it to an new variable:
private Settings settings = Config.Properties.Settings.Default;
But with the Localization i cant to that to shorten it up.
Try´d the following:
private Type locals = CodepackConfig.Localization;
Label.Text = locals.MyString
Does somebody have hint for me how i can set a Variable to "Config.Localization"? And yes, i just code C# for 1 Month now, looks like a stupidly easy error :)
Check out the using directive's documentation.
I assume you're interested in something like
using Localization = Config.Properties.Localization;
1: https://msdn.microsoft.com/en-us/library/sf0df423.aspx?f=255&MSPPError=-2147217396

How can I modify what gets copied to the Clipboard?

I'm copying a DataGrid to the clipboard so that it can be pasted to e.g. Excel while maintaining its format like this:
MyDataGrid.SelectAllCells();
MyDataGrid.ClipboardCopyMode = DataGridClipboardCopyMode.IncludeHeader;
ApplicationCommands.Copy.Execute(null, MyDataGrid);
This works very nice. However, I need to add the string "MyDataGridTitle". If pasted to Excel, this should simply stand above the DataGrid.
I have tried various ways (e.g. use a DataObject) and tortured google, but to no success. I'd be thankful for a hint, tip or answer!
This is not a very elegant solution, but you can try by manipulating the html string generated by your DataGrid (indeed when you paste in Excel, the DataFormats.Html format is the one which is used).
Something like this:
dg.SelectAllCells();
dg.ClipboardCopyMode = DataGridClipboardCopyMode.IncludeHeader;
ApplicationCommands.Copy.Execute(null, dg);
string dataGridContent = (string)Clipboard.GetData(DataFormats.Html);
dataGridContent = dataGridContent.Replace("<TABLE>",
String.Format("<TABLE><TR><TD colspan='{0}'>Your additional text<TD></TR>", dg.Columns.Count));
Clipboard.SetText(dataGridContent, TextDataFormat.Html);
Of course you can improve this code for example by using regular expressions instead of the Replace method.

How to set Specific words font style quickly

Microsoft word didn't provide a spelling tool for my language and I want to do it with my self, every this is going fine and there is a small problem I need to solve.
In order to spell check for each words I need to get each words first. obviously if I used
var doc = Globals.ThisAddIn.Application.ActiveDocument;
int counts = doc.Words.Count;
List<string> words=new List<string>;
for (int i=0;i<counts;i++)
{
words.Add(doc.Words[i].Text.Trim());
}
to get each words, it will run very slowly.
after that I write my own method to get each words, it runs good. after that I need to mark the wrong spelled words. I used
bool check = diccheck(word);
if (check == false)
{
doc.Words[i].Font.Underline = Word.WdUnderline.wdUnderlineWavy;
doc.Words[i].Font.UnderlineColor = Word.WdColor.wdColorRed;
}
and it is run very slow again. can any one help me show the way how to mark the wrong spelled words quickly?
What do you think about a thread runs while you writes?
Are you trying to correct wrong words?
If i get it right, may be more usefull if you only checks the text that changed.

converting filenames to lower case

was hoping for some advice as to how to convert existing file names in a folder...all to lower case.
I felt that a good start would be to save the file names in a list and convert them all to lower.
How can I replace the existing file names in the folder to the lower case ones?
List<string> codes = new List<string>();
string[]productCodes = Directory.GetFiles(#"C:\Users\Ariang\Desktop\screenshotslowercase\screenshots");
codes = productCodes.ToList();
codes = codes.ConvertAll(t => t.ToLower());
This should work:
foreach (var file in Directory.GetFiles(#"C:\Temp\testrename"))
{
File.Move(file, file.ToLowerInvariant());
}
A few notes, first of all I have tested this and it works, somebody else mentioned using a temporary variable, but I haven't needed to do this.
Also, I have run this multiple times on the same directory, and I don't get an IOException the second or third time around, so I don't think any additional checking is necessary.
However, I am on Windows 8 and targeting .Net 4.5, things may be different on earlier versions of Windows or .Net.
Windows system doesn't see difference betweeen lower and upper letters in file names. Thats why you can't convert like "MyFile" -> "myfile". Use two steps instead:
foreach (var file in Directory.GetFiles(#"C:\Temp\testrename"))
{
var tempName = "." + file.ToLowerInvariant();
File.Move(file, tempName);
File.Move(tempName, file.ToLowerInvariant());
}
no need for list and all that. Simple read the file name from directory and use
System.IO.File.Move("oldfilename", "oldfilename".ToLower());
string[] files = Directory.GetFiles(dir);
foreach(string file in files)
{
System.IO.File.Move(file, file.ToLowerInvariant());
}

Categories