我正在嘗試將檔案從 Internet 下載到具有 .net core 的本地檔案夾,盡管它在編譯時沒有警告或錯誤,但在執行時顯示錯誤
using System;
namespace download_file
{
class Program
{
static void Main(string[] args)
{
string zip_url = @"https://www.python.org/ftp/python/3.9.5/python-3.9.5-embed-amd64.zip";
string current_directory = System.IO.Directory.GetCurrentDirectory();
string dl_path = System.IO.Directory.CreateDirectory(System.String.Format(@"{0}\DL", current_directory)).FullName;
string[] zip_url_words = zip_url.Split('/', 1);
string downloaded_file_path = System.String.Format(@"{0}\{1}", dl_path, zip_url_words[zip_url_words.Length - 1]);
// downloading the embedded python zip file
file_download(zip_url, downloaded_file_path);
void file_download(string url, string save_path)
{
if (url is null)
{
return;
}
string[] url_word_array = url.Split('/', 1);
if (save_path is null)
{
save_path = System.IO.Directory.GetCurrentDirectory() @"\" url_word_array[url_word_array.Length - 1]; //rsplit
}
System.Net.WebClient client = new System.Net.WebClient();
client.DownloadFile(url, save_path);
//System.Console.WriteLine("Press any key...")
//System.Console.ReadLine()
}
}
}
}

uj5u.com熱心網友回復:
我變了
string[] zip_url_words = zip_url.Split('/', 1);
到
string[] zip_url_words = zip_url.Split('/', '1');
和
string[] url_word_array = url.Split('/', 1);
到
string[] url_word_array = url.Split('/', '1');
我下載了
你能再試一次嗎?
uj5u.com熱心網友回復:
問題出在這一行:
string[] zip_url_words = zip_url.Split('/', 1);
從檔案:
public String[] Split(char[]? separator, int count);
//
// Summary:
// Splits a string into a maximum number substrings based on the provided character
// separator.
因此上面的代碼["https://www.python.org/ftp/python/3.9.5/python-3.9.5-embed-amd64.zip"]作為變數的值回傳zip_url_words。像這樣更改上面的代碼:
string[] zip_url_words = zip_url.Split('/');
我進行了更改,代碼對我有用。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/317061.html
上一篇:.net中對單例物件的多次訪問
