while (true)
{
try
{
File.WriteAllText(@"C:\\serv1.txt", "text");
break;
}
catch (Exception)
{
Thread.Sleep(300);
}
}
void UntilSuccess(Action action) {
if (action == null)
throw new ArgumentNullException();
while (true) {
try
{
action.Invoke();
break;
}
catch { Thread.Sleep(300); }
}
}
// Использовать так:
UntilSuccess(() => File.WriteAllText(@"C:\\serv1.txt", "text"));