我有一個問題,我試圖連續下載兩個文件WebClient
,第二個文件失敗。該文件位於運行IIS的127.0.0.2/files/中。目錄瀏覽已打開,我可以看到該位置的文件並通過瀏覽器下載。使用WebClient下載C#錯誤
FileDownload()
WebClass
在窗體加載時運行,併成功下載state.xml。我可以編輯這個文件,然後檢查下載目錄(應用程序路徑)文件已經改變。然後,表單加載運行ReadXml()
,並且如果<action>
是true
在此文件中,則下載與<file>
一起運行,並且這是失敗的位置。我檢查目錄和文件不存在。 如果您在下載行中暫停程序,您將會看到第二次運行時出現未知WebException。
我不知道如何使這個問題更簡單,但我想我已經刪除了所有與問題無關的代碼。我通過幾個消息框來檢查進度,並且您將從中看到該文件位於網址上,但在本地找不到。我已經排除了文件夾的權限,因爲xml文件的下載質量很好,而且我最初使用的是一個批處理文件,只是簡單的壓縮文件,但切換到文本文件以防安全問題。
另外值得注意的是:當我暫停程序下載文件時,所有的變量都有正確的讀取並複製到線路client.DownloadFile(strDownUrl, Application.StartupPath + @"\" + strSaveFile);
中的鏈接到我的瀏覽器中,帶我到那個文件。
編輯:網絡例外:EX { 「一個Web客戶端請求期間發生異常。」} System.Net.WebException
的InnerException System.Exception的{{系統 「不支持給定路徑的格式。」}。 NotSupportedException異常}
下面是我的代碼:
Form1.cs的
namespace Test
{
private void button1_Click(object sender, EventArgs e)
{
WebClass.FileDownload(Globals.urlFiles + Globals.xmlPath, Globals.xmlPath);
Main.ReadXml();
}
}
WebClass.cs
namespace Test
{
class WebClass
{
public static void FileDownload(string strDownUrl, string strSaveFile)
{
//var strDownUrl = Globals.url + Globals.xmlPath;
MessageBox.Show(strDownUrl);
var flag = 0;
System.Net.HttpWebRequest request = null;
System.Net.HttpWebResponse response = null;
request = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(strDownUrl);
request.Timeout = 30000;
try
{
response = (System.Net.HttpWebResponse)request.GetResponse();
flag = 1;
}
catch
{
flag = -1;
}
if (flag == 1)
{
MessageBox.Show("File Found!");
}
else
{
MessageBox.Show("File Not Found!");
}
using (WebClient client = new WebClient())
{
try
{
client.DownloadFile(strDownUrl, Application.StartupPath + @"\" + strSaveFile);
}
catch (WebException ex)
{
var n = ex.Status;
MessageBox.Show("Cannot find " + strDownUrl);
}
}
}
}
}
Main.cs
namespace Test
{
public static class Globals
{
private static string prUrl = "http://127.0.0.2";
public static string url{ get { return prUrl; } set { prUrl = value; } }
private static string prUrlFiles = "http://127.0.0.2/files/";
public static string urlFiles { get { return prUrlFiles; } set { prUrlFiles = value; } }
private static string prXmlPath = "state.xml";
public static string xmlPath { get { return prXmlPath; } set { prXmlPath = value; } }
}
class Main
{
public static void ReadXml()
{
double xVersion;
bool xAction, xArchive;
string xFile;
XmlDocument doc = new XmlDocument();
doc.Load(Application.StartupPath + @"\" + Globals.xmlPath);
//GET XML VALUES
xVersion = Convert.ToDouble(doc.SelectSingleNode("XML/Program/Version").InnerText);
xAction = Convert.ToBoolean(doc.SelectSingleNode("XML/Program/Action").InnerText);
xArchive = Convert.ToBoolean(doc.SelectSingleNode("XML/Program/Action").InnerText);
xFile = Convert.ToString(doc.SelectSingleNode("XML/Request/Command/File").InnerText);
if (xAction == true)
{
MessageBox.Show("Action");
if (xFile != "")
{
WebClass.FileDownload(Globals.urlFiles + xFile, Application.StartupPath + @"\" + xFile);
}
}
}
}
}
state.xml
<?xml version="1.0" ?>
<XML>
<Program>
<Version>0.1</Version>
<Action>True</Action>
</Program>
<Request>
<Number>1</Number>
<Serial_Number></Serial_Number>
<User></User>
<Command>
<File>test.txt</File>
<Archive>False</Archive>
</Command>
</Request>
</XML>
所以當你解析從XML文件並運行WebClass.FileDownload您收到消息框,說: MessageBox.Show(「文件中找到!」) 我說得對不對? – anderhil
這將是也很好武官什麼未知引發WebException說 – anderhil
@anderhill你是正確的。編輯:除了內容 異常說不受支持的路徑格式,但我不能工作了,爲什麼因爲每個文件的路徑是相同的。 –