3

I've been working on a project lately & I'm totally new to .NET so excuse me for that -.-

I was wondering how to replace the value of a string with the content of a .txt file. This is what I've got so far but it won't work -_- Instead of showing me the content of the file it shows me the path as I wrote it.

string replacedString = string1.Replace(string1, Environment.CurrentDirectory + @"\scripts\StickyNotes\Server Website.txt");

The string1 was in fact a string array that I converted. But I don't think this should make any difference because it's equal to a string now. Btw this string contains now a value equal to 1 line of text. So what the .txt file should do is overwriting that line with the new value.

Maybe there isn't a direct option to do that so what I'm asking you is maybe a little complicated -.-

Because I'm new to C# coding could you also explain what the code of your answer is supposed to do xP

Thanx for taking your time!!

Edit: I also wan't to note that I'm actually working in a public StickyNotes(). So no static class.

3
  • so you don't care about the value of string1 anymore? or you want to replace something inside of string1 with the file contents? Commented Aug 29, 2017 at 17:56
  • Yes @MichaelSharp Commented Aug 29, 2017 at 17:56
  • I don't care about the values in the string1 just wan't to replace them with the file contents Commented Aug 29, 2017 at 17:57

1 Answer 1

2

You should be able to just do string replacedString = File.ReadAllText(@"\scripts\StickyNotes\Server Website.txt"); This will read in the file, and create a new variable called replacedString to store the file contents.

If you want to overwrite string1, then assign the return value from File.ReadAllText to that instead of creating a new variable replacedString. It would look like this string1 = File.ReadAllText(@"\scripts\StickyNotes\Server Website.txt");

File.ReadAllText() Takes in the path of the file you want to read, and then returns the whole file as a string. (https://msdn.microsoft.com/en-us/library/system.io.file.readalltext(v=vs.110).aspx)

the string.Replace() method you were using, "Returns a new string in which all occurrences of a specified string in the current instance are replaced with another specified string." (https://msdn.microsoft.com/en-us/library/fk49wtc1(v=vs.110).aspx) Essentially, it looks for the first value you passed in, and replaces it with the second one.

Sign up to request clarification or add additional context in comments.

2 Comments

But this won't overwrite the content of my string1 right?
It can do either depending on which variable you assign the results to. I wasn't sure if you wanted to overwrite it or not, which is why I explained both.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.