Вы делаете его корректный путь, но пользователи могут предоставлять URL сайтам, которым установили недопустимые сертификаты SSL. Можно проигнорировать те проблемы сертификата, если Вы вставляете эту строку перед созданием фактического веб-запроса:
ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(AcceptAllCertifications);
, где AcceptAllCertifications
определяется как
public bool AcceptAllCertifications(object sender, System.Security.Cryptography.X509Certificates.X509Certificate certification, System.Security.Cryptography.X509Certificates.X509Chain chain, System.Net.Security.SslPolicyErrors sslPolicyErrors)
{
return true;
}
Такая функция сделает это:
public static bool IsFileReady(string filename)
{
// If the file can be opened for exclusive access it means that the file
// is no longer locked by another process.
try
{
using (FileStream inputStream = File.Open(filename, FileMode.Open, FileAccess.Read, FileShare.None))
return inputStream.Length > 0;
}
catch (Exception)
{
return false;
}
}
Вставьте ее в цикл while
, и у вас будет что-то, что будет блокироваться, пока файл не станет доступным:
public static void WaitForFile(string filename)
{
//This will lock the execution until the file is ready
//TODO: Add some logic to make it async and cancelable
while (!IsFileReady(filename)) { }
}
Нет функции, которая позволила бы вам ждать, когда конкретный дескриптор / расположение файловой системы станет доступным для записи. К сожалению, все, что вы можете сделать, это запросить ручку на предмет записи.
bool isLocked = true;
while (isLocked)
try {
System.IO.File.Move(filename, filename2);
isLocked = false;
}
catch { }
System.IO.File.Move(filename2, filename);