Safe way to find a string within files in Win10
An application we use saves out a configuration string as XML with the tag <connectionStrings>
. I'm writing a backup script that needs to know that DB that's pointed to. I've found this string in different files on different machines, depending on how it was installed.
To start with, I tried simply using Win10's Search in Explorer. This fails to find the string no matter what options I try. I tried walking up and down the directory tree, even selecting the folder the text file is in, and it still can't find it. I have all the search options turned on, any ideas?
But my main question is whether there is now a canonical solution for finding-strings-in-files within .Net? I find many examples here, but they generally use external shelled utilities, or just read every file and search. One interesting solution used an external indexer, but that's outside the scope of this project.
.net full-text-search
add a comment |
An application we use saves out a configuration string as XML with the tag <connectionStrings>
. I'm writing a backup script that needs to know that DB that's pointed to. I've found this string in different files on different machines, depending on how it was installed.
To start with, I tried simply using Win10's Search in Explorer. This fails to find the string no matter what options I try. I tried walking up and down the directory tree, even selecting the folder the text file is in, and it still can't find it. I have all the search options turned on, any ideas?
But my main question is whether there is now a canonical solution for finding-strings-in-files within .Net? I find many examples here, but they generally use external shelled utilities, or just read every file and search. One interesting solution used an external indexer, but that's outside the scope of this project.
.net full-text-search
add a comment |
An application we use saves out a configuration string as XML with the tag <connectionStrings>
. I'm writing a backup script that needs to know that DB that's pointed to. I've found this string in different files on different machines, depending on how it was installed.
To start with, I tried simply using Win10's Search in Explorer. This fails to find the string no matter what options I try. I tried walking up and down the directory tree, even selecting the folder the text file is in, and it still can't find it. I have all the search options turned on, any ideas?
But my main question is whether there is now a canonical solution for finding-strings-in-files within .Net? I find many examples here, but they generally use external shelled utilities, or just read every file and search. One interesting solution used an external indexer, but that's outside the scope of this project.
.net full-text-search
An application we use saves out a configuration string as XML with the tag <connectionStrings>
. I'm writing a backup script that needs to know that DB that's pointed to. I've found this string in different files on different machines, depending on how it was installed.
To start with, I tried simply using Win10's Search in Explorer. This fails to find the string no matter what options I try. I tried walking up and down the directory tree, even selecting the folder the text file is in, and it still can't find it. I have all the search options turned on, any ideas?
But my main question is whether there is now a canonical solution for finding-strings-in-files within .Net? I find many examples here, but they generally use external shelled utilities, or just read every file and search. One interesting solution used an external indexer, but that's outside the scope of this project.
.net full-text-search
.net full-text-search
asked Nov 26 '18 at 20:49
Maury MarkowitzMaury Markowitz
3,79062863
3,79062863
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
The short answer is No. There is no prescribed way to perform this in C#, because the way you go about it is going to vary depending on your use case. However, there are plenty of options out there for performing this type of operation.
To start, let's consider that if we want to search for contents in a file, at some point we had to open the file and look at its contents. You mentioned in your last paragraph the concept of using an external indexer, which would do exactly this. Funny enough, that is the exact same thing that Windows Search is going to do, so let's start by looking at that.
When you perform a Windows Search it uses the Search Index to lookup files. If you aren't finding the files you are searching for, there are a few possible reasons for this.
- Search Indexing has been disabled entirely.
- Search Indexing is not running on the folder containing your config files.
- Search Indexing is not configured to scan files with your configuration's extension.
Assuming all of these things are configured correctly, you should see results when performing a search. However, when performing a search for connectionStrings
on my machine, I didn't get any of my expected web.config files back. When digging a little deeper, Windows Search is only configured to Index Properties Only
for .config
files instead of Index Properties and File Contents
. There is probably a good security reason for why you shouldn't index these files as well but I will leave that for another post.
Overall though, I think trying to use Windows Search or another library to do this is overkill for such a basic task. I assume the following.
- You know the general location (or parent folder) where all of these config files reside. Even if they are nested, you are probably within 2-3 levels of each config file.
- You know the extension(s) of the config file you are searching for and can add that to a white list.
Assuming you know these two things, the efficiency of finding and searching the files should be fine. You would want to follow a pattern as outlined below.
- Select the root folder
- List the files in the current folder. Select and files with extension(s) that match your whitelist.
- Read the file contents and look for your string. You could do this using buffers but you will need to have a bit more complex logic for edge cases (where your search term overlaps the edge of your buffer). This shouldn't be necessary unless these are much more than just config files.
- Any files found to contain your search term you perform your required action on.
- Now, scan the current folder for other folders. Recursively process each folder, looping back to Step #2 each time.
To enhance this solution, you could use a temporary cache where you keep track of each matching file you find that needs to be backed up. You could save this cache to a file (along with a timestamp) and only re-scan after a set period of time. This way for subsequent runs of your backup utility, you don't actually have to search the file system you simply backup files.
Another option would be to store a blacklist of all "false positive" files so you don't search the contents of the files in a future run through.
I hope this helps and if you have any questions please let me know.
Superb post Shawn. I'll digest it... but it looks like I'll just use file-search in the likely locations and the use MS's XML to parse it.
– Maury Markowitz
Nov 27 '18 at 14:52
Sounds great Maury! What type of config files are these? Typical web.config or app.config files? Or are these custom XML configurations?
– Shawn Lehner
Nov 27 '18 at 15:24
Web.config. It's pointing from a client to an appserver to a DB, and my goal is to back up that DB.
– Maury Markowitz
Nov 27 '18 at 15:26
Great. Take a look at this example of how to load a web.config once you find it in C#. This would be the easiest way to read the data out of it. stackoverflow.com/questions/4738/…
– Shawn Lehner
Nov 27 '18 at 15:30
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53488832%2fsafe-way-to-find-a-string-within-files-in-win10%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The short answer is No. There is no prescribed way to perform this in C#, because the way you go about it is going to vary depending on your use case. However, there are plenty of options out there for performing this type of operation.
To start, let's consider that if we want to search for contents in a file, at some point we had to open the file and look at its contents. You mentioned in your last paragraph the concept of using an external indexer, which would do exactly this. Funny enough, that is the exact same thing that Windows Search is going to do, so let's start by looking at that.
When you perform a Windows Search it uses the Search Index to lookup files. If you aren't finding the files you are searching for, there are a few possible reasons for this.
- Search Indexing has been disabled entirely.
- Search Indexing is not running on the folder containing your config files.
- Search Indexing is not configured to scan files with your configuration's extension.
Assuming all of these things are configured correctly, you should see results when performing a search. However, when performing a search for connectionStrings
on my machine, I didn't get any of my expected web.config files back. When digging a little deeper, Windows Search is only configured to Index Properties Only
for .config
files instead of Index Properties and File Contents
. There is probably a good security reason for why you shouldn't index these files as well but I will leave that for another post.
Overall though, I think trying to use Windows Search or another library to do this is overkill for such a basic task. I assume the following.
- You know the general location (or parent folder) where all of these config files reside. Even if they are nested, you are probably within 2-3 levels of each config file.
- You know the extension(s) of the config file you are searching for and can add that to a white list.
Assuming you know these two things, the efficiency of finding and searching the files should be fine. You would want to follow a pattern as outlined below.
- Select the root folder
- List the files in the current folder. Select and files with extension(s) that match your whitelist.
- Read the file contents and look for your string. You could do this using buffers but you will need to have a bit more complex logic for edge cases (where your search term overlaps the edge of your buffer). This shouldn't be necessary unless these are much more than just config files.
- Any files found to contain your search term you perform your required action on.
- Now, scan the current folder for other folders. Recursively process each folder, looping back to Step #2 each time.
To enhance this solution, you could use a temporary cache where you keep track of each matching file you find that needs to be backed up. You could save this cache to a file (along with a timestamp) and only re-scan after a set period of time. This way for subsequent runs of your backup utility, you don't actually have to search the file system you simply backup files.
Another option would be to store a blacklist of all "false positive" files so you don't search the contents of the files in a future run through.
I hope this helps and if you have any questions please let me know.
Superb post Shawn. I'll digest it... but it looks like I'll just use file-search in the likely locations and the use MS's XML to parse it.
– Maury Markowitz
Nov 27 '18 at 14:52
Sounds great Maury! What type of config files are these? Typical web.config or app.config files? Or are these custom XML configurations?
– Shawn Lehner
Nov 27 '18 at 15:24
Web.config. It's pointing from a client to an appserver to a DB, and my goal is to back up that DB.
– Maury Markowitz
Nov 27 '18 at 15:26
Great. Take a look at this example of how to load a web.config once you find it in C#. This would be the easiest way to read the data out of it. stackoverflow.com/questions/4738/…
– Shawn Lehner
Nov 27 '18 at 15:30
add a comment |
The short answer is No. There is no prescribed way to perform this in C#, because the way you go about it is going to vary depending on your use case. However, there are plenty of options out there for performing this type of operation.
To start, let's consider that if we want to search for contents in a file, at some point we had to open the file and look at its contents. You mentioned in your last paragraph the concept of using an external indexer, which would do exactly this. Funny enough, that is the exact same thing that Windows Search is going to do, so let's start by looking at that.
When you perform a Windows Search it uses the Search Index to lookup files. If you aren't finding the files you are searching for, there are a few possible reasons for this.
- Search Indexing has been disabled entirely.
- Search Indexing is not running on the folder containing your config files.
- Search Indexing is not configured to scan files with your configuration's extension.
Assuming all of these things are configured correctly, you should see results when performing a search. However, when performing a search for connectionStrings
on my machine, I didn't get any of my expected web.config files back. When digging a little deeper, Windows Search is only configured to Index Properties Only
for .config
files instead of Index Properties and File Contents
. There is probably a good security reason for why you shouldn't index these files as well but I will leave that for another post.
Overall though, I think trying to use Windows Search or another library to do this is overkill for such a basic task. I assume the following.
- You know the general location (or parent folder) where all of these config files reside. Even if they are nested, you are probably within 2-3 levels of each config file.
- You know the extension(s) of the config file you are searching for and can add that to a white list.
Assuming you know these two things, the efficiency of finding and searching the files should be fine. You would want to follow a pattern as outlined below.
- Select the root folder
- List the files in the current folder. Select and files with extension(s) that match your whitelist.
- Read the file contents and look for your string. You could do this using buffers but you will need to have a bit more complex logic for edge cases (where your search term overlaps the edge of your buffer). This shouldn't be necessary unless these are much more than just config files.
- Any files found to contain your search term you perform your required action on.
- Now, scan the current folder for other folders. Recursively process each folder, looping back to Step #2 each time.
To enhance this solution, you could use a temporary cache where you keep track of each matching file you find that needs to be backed up. You could save this cache to a file (along with a timestamp) and only re-scan after a set period of time. This way for subsequent runs of your backup utility, you don't actually have to search the file system you simply backup files.
Another option would be to store a blacklist of all "false positive" files so you don't search the contents of the files in a future run through.
I hope this helps and if you have any questions please let me know.
Superb post Shawn. I'll digest it... but it looks like I'll just use file-search in the likely locations and the use MS's XML to parse it.
– Maury Markowitz
Nov 27 '18 at 14:52
Sounds great Maury! What type of config files are these? Typical web.config or app.config files? Or are these custom XML configurations?
– Shawn Lehner
Nov 27 '18 at 15:24
Web.config. It's pointing from a client to an appserver to a DB, and my goal is to back up that DB.
– Maury Markowitz
Nov 27 '18 at 15:26
Great. Take a look at this example of how to load a web.config once you find it in C#. This would be the easiest way to read the data out of it. stackoverflow.com/questions/4738/…
– Shawn Lehner
Nov 27 '18 at 15:30
add a comment |
The short answer is No. There is no prescribed way to perform this in C#, because the way you go about it is going to vary depending on your use case. However, there are plenty of options out there for performing this type of operation.
To start, let's consider that if we want to search for contents in a file, at some point we had to open the file and look at its contents. You mentioned in your last paragraph the concept of using an external indexer, which would do exactly this. Funny enough, that is the exact same thing that Windows Search is going to do, so let's start by looking at that.
When you perform a Windows Search it uses the Search Index to lookup files. If you aren't finding the files you are searching for, there are a few possible reasons for this.
- Search Indexing has been disabled entirely.
- Search Indexing is not running on the folder containing your config files.
- Search Indexing is not configured to scan files with your configuration's extension.
Assuming all of these things are configured correctly, you should see results when performing a search. However, when performing a search for connectionStrings
on my machine, I didn't get any of my expected web.config files back. When digging a little deeper, Windows Search is only configured to Index Properties Only
for .config
files instead of Index Properties and File Contents
. There is probably a good security reason for why you shouldn't index these files as well but I will leave that for another post.
Overall though, I think trying to use Windows Search or another library to do this is overkill for such a basic task. I assume the following.
- You know the general location (or parent folder) where all of these config files reside. Even if they are nested, you are probably within 2-3 levels of each config file.
- You know the extension(s) of the config file you are searching for and can add that to a white list.
Assuming you know these two things, the efficiency of finding and searching the files should be fine. You would want to follow a pattern as outlined below.
- Select the root folder
- List the files in the current folder. Select and files with extension(s) that match your whitelist.
- Read the file contents and look for your string. You could do this using buffers but you will need to have a bit more complex logic for edge cases (where your search term overlaps the edge of your buffer). This shouldn't be necessary unless these are much more than just config files.
- Any files found to contain your search term you perform your required action on.
- Now, scan the current folder for other folders. Recursively process each folder, looping back to Step #2 each time.
To enhance this solution, you could use a temporary cache where you keep track of each matching file you find that needs to be backed up. You could save this cache to a file (along with a timestamp) and only re-scan after a set period of time. This way for subsequent runs of your backup utility, you don't actually have to search the file system you simply backup files.
Another option would be to store a blacklist of all "false positive" files so you don't search the contents of the files in a future run through.
I hope this helps and if you have any questions please let me know.
The short answer is No. There is no prescribed way to perform this in C#, because the way you go about it is going to vary depending on your use case. However, there are plenty of options out there for performing this type of operation.
To start, let's consider that if we want to search for contents in a file, at some point we had to open the file and look at its contents. You mentioned in your last paragraph the concept of using an external indexer, which would do exactly this. Funny enough, that is the exact same thing that Windows Search is going to do, so let's start by looking at that.
When you perform a Windows Search it uses the Search Index to lookup files. If you aren't finding the files you are searching for, there are a few possible reasons for this.
- Search Indexing has been disabled entirely.
- Search Indexing is not running on the folder containing your config files.
- Search Indexing is not configured to scan files with your configuration's extension.
Assuming all of these things are configured correctly, you should see results when performing a search. However, when performing a search for connectionStrings
on my machine, I didn't get any of my expected web.config files back. When digging a little deeper, Windows Search is only configured to Index Properties Only
for .config
files instead of Index Properties and File Contents
. There is probably a good security reason for why you shouldn't index these files as well but I will leave that for another post.
Overall though, I think trying to use Windows Search or another library to do this is overkill for such a basic task. I assume the following.
- You know the general location (or parent folder) where all of these config files reside. Even if they are nested, you are probably within 2-3 levels of each config file.
- You know the extension(s) of the config file you are searching for and can add that to a white list.
Assuming you know these two things, the efficiency of finding and searching the files should be fine. You would want to follow a pattern as outlined below.
- Select the root folder
- List the files in the current folder. Select and files with extension(s) that match your whitelist.
- Read the file contents and look for your string. You could do this using buffers but you will need to have a bit more complex logic for edge cases (where your search term overlaps the edge of your buffer). This shouldn't be necessary unless these are much more than just config files.
- Any files found to contain your search term you perform your required action on.
- Now, scan the current folder for other folders. Recursively process each folder, looping back to Step #2 each time.
To enhance this solution, you could use a temporary cache where you keep track of each matching file you find that needs to be backed up. You could save this cache to a file (along with a timestamp) and only re-scan after a set period of time. This way for subsequent runs of your backup utility, you don't actually have to search the file system you simply backup files.
Another option would be to store a blacklist of all "false positive" files so you don't search the contents of the files in a future run through.
I hope this helps and if you have any questions please let me know.
answered Nov 27 '18 at 1:15
Shawn LehnerShawn Lehner
1,260712
1,260712
Superb post Shawn. I'll digest it... but it looks like I'll just use file-search in the likely locations and the use MS's XML to parse it.
– Maury Markowitz
Nov 27 '18 at 14:52
Sounds great Maury! What type of config files are these? Typical web.config or app.config files? Or are these custom XML configurations?
– Shawn Lehner
Nov 27 '18 at 15:24
Web.config. It's pointing from a client to an appserver to a DB, and my goal is to back up that DB.
– Maury Markowitz
Nov 27 '18 at 15:26
Great. Take a look at this example of how to load a web.config once you find it in C#. This would be the easiest way to read the data out of it. stackoverflow.com/questions/4738/…
– Shawn Lehner
Nov 27 '18 at 15:30
add a comment |
Superb post Shawn. I'll digest it... but it looks like I'll just use file-search in the likely locations and the use MS's XML to parse it.
– Maury Markowitz
Nov 27 '18 at 14:52
Sounds great Maury! What type of config files are these? Typical web.config or app.config files? Or are these custom XML configurations?
– Shawn Lehner
Nov 27 '18 at 15:24
Web.config. It's pointing from a client to an appserver to a DB, and my goal is to back up that DB.
– Maury Markowitz
Nov 27 '18 at 15:26
Great. Take a look at this example of how to load a web.config once you find it in C#. This would be the easiest way to read the data out of it. stackoverflow.com/questions/4738/…
– Shawn Lehner
Nov 27 '18 at 15:30
Superb post Shawn. I'll digest it... but it looks like I'll just use file-search in the likely locations and the use MS's XML to parse it.
– Maury Markowitz
Nov 27 '18 at 14:52
Superb post Shawn. I'll digest it... but it looks like I'll just use file-search in the likely locations and the use MS's XML to parse it.
– Maury Markowitz
Nov 27 '18 at 14:52
Sounds great Maury! What type of config files are these? Typical web.config or app.config files? Or are these custom XML configurations?
– Shawn Lehner
Nov 27 '18 at 15:24
Sounds great Maury! What type of config files are these? Typical web.config or app.config files? Or are these custom XML configurations?
– Shawn Lehner
Nov 27 '18 at 15:24
Web.config. It's pointing from a client to an appserver to a DB, and my goal is to back up that DB.
– Maury Markowitz
Nov 27 '18 at 15:26
Web.config. It's pointing from a client to an appserver to a DB, and my goal is to back up that DB.
– Maury Markowitz
Nov 27 '18 at 15:26
Great. Take a look at this example of how to load a web.config once you find it in C#. This would be the easiest way to read the data out of it. stackoverflow.com/questions/4738/…
– Shawn Lehner
Nov 27 '18 at 15:30
Great. Take a look at this example of how to load a web.config once you find it in C#. This would be the easiest way to read the data out of it. stackoverflow.com/questions/4738/…
– Shawn Lehner
Nov 27 '18 at 15:30
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53488832%2fsafe-way-to-find-a-string-within-files-in-win10%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown