"How would I write a loop that replaces each number with the next highest number up until, say Server 10?"
You can set the server name in a loop, using the loop iterator value as part of the server name
for(int i = 1; i <= 10; i++)
{
txtTextBox.Text = File.Exists($@"\\Server{i}\documents\File.txt")
? $"Server{i} -- File copy successful."
: $"Server{i} -- File copy unsuccessful";
}
Note that the code above will overwrite the txtTextBox.Text value on each iteration. You may instead want to capture all the statuses in the loop and then display them at the end:
txtTextBox.Text = string.Join(Environment.NewLine, Enumerable.Range(1, 10)
.Select(i => File.Exists($@"\\Server{i}\documents\File.txt")
? $"Server{i} -- File copy successful."
: $"Server{i} -- File copy unsuccessful."));
In the comments you asked:
"How would you do this if the file location was in a variable?"
One way to do this is to use a format string with a placeholder ({0}) where the number would go, and then use string.Format to fill in that placeholder inside the loop.
We can extract the server name from this string using string.Split on the \ character and grabbing the first item.
For example:
var serverPath = @"\\Server{0}\documents\File.txt";
txtTextBox.Text = string.Join(Environment.NewLine, Enumerable.Range(1, 10)
.Select(i =>
{
var thisPath = string.Format(serverPath, i);
var serverName = thisPath.Split(new[] { '\\' },
StringSplitOptions.RemoveEmptyEntries).First();
return File.Exists(thisPath)
? $"{serverName} -- File copy successful."
: $"{serverName} -- File copy unsuccessful.";
}));