"WebClient.DownloadFile" 在我向它传递参数时抛出错误,但如果我对值进行硬编码则工作正常
"WebClient.DownloadFile" throws an error when I pass arguments to it but works fine if I hardcode the values
我有一个使用 .NET 4.6.1 框架的 C# exe。我从 C++ exe 启动这个 exe,因为这是要求。它以前有效,但突然停止按预期方式工作。
我用 5 个参数启动 exe。我检查了接收到的参数的值和长度,当 C# exe 接收到它们时,它们似乎都是正确的,但是当涉及到 DownloadFile
方法时,它似乎抛出异常。
另一方面,如果我在命令提示符中硬编码值或 运行 exe 并将参数传递给它似乎工作正常。
一旦从 C++ exe 启动它,它就会将其从堆栈跟踪写入日志,我将其打印到
username is: abc uname length is: 3| password is: abc@123 pass length
is: 7| patch download from path is: .1.1.119\Backup_share\patch
testing\Skype-8.81.0.268.zip FileToDownload length is: 60| patch
download to path is: E:\Test\Patch.zip localDestinationFilePath length
is: 17
System.Net.WebException Exception in DownloadPFile(
at System.Net.WebClient.DownloadFile(Uri address, String fileName)
at System.Net.WebClient.DownloadFile(String address, String fileName)
at
IPMPlusAlternatePatchDownloader.IPMPlusPatchDownloader.DownloadPFile(String userName, String password, String FileToDownload, String localDestinationFilePath)
这是我正在使用的代码
static void Main(string[] args)
{
PacthDownload.DownloadPFile(args[1], args[2], args[3], args[4]);
}
private void DownloadFileShareAndFTP(string userName, string password, string FileToDownload, string localDestinationFilePath)
{
WebClient client = new WebClient();
client.Credentials = new NetworkCredential(userName, password);
client.DownloadFile(FileToDownload, localDestinationFilePath);
}
这是我对值进行硬编码而不是从参数中获取它们时的样子
static void Main(string[] args)
{
PacthDownload.DownloadFileShareAndFTP("abc", "abc@123",@"\10.1.1.119\Backup_share\patch testing\Skype-8.81.0.268.zip", @"E:\Test\Patch.zip");
//PacthDownload.DownloadPFile(args[1], args[2], args[3], args[4]);
}
private void DownloadFileShareAndFTP(string userName, string password, string FileToDownload, string localDestinationFilePath)
{
WebClient client = new WebClient();
client.Credentials = new NetworkCredential(userName, password);
client.DownloadFile(FileToDownload, localDestinationFilePath);
}
username is: abc uname length is: 3| password is: abc@123 pass length
is: 7| patch download from path is: .1.1.119\Backup_share\patch
testing\Skype-8.81.0.268.zip FileToDownload length is: 60| patch
download to path is: E:\Test\Patch.zip localDestinationFilePath length
is: 17
DownloadPFile EOF
长度是参数的长度,而不是我硬编码的值。
这是文件下载后的截图,但由于某种原因,它只是
我为此使用 visual studio 2017。
我在此处添加代码时删除了代码中的日志语句,以使其看起来更清晰。
关于如何解决这个问题有什么想法吗?
您收到的包含此路径的参数 "patch download from path is: .1.1.119\Backup_share\patch testing\Skype-8.81.0.268.zip"
似乎不包含“\”字符的字符的任何转义序列,这可能是您收到异常的原因。在每个应该修复它的“\”字符后添加一个额外的“\”。
我有一个使用 .NET 4.6.1 框架的 C# exe。我从 C++ exe 启动这个 exe,因为这是要求。它以前有效,但突然停止按预期方式工作。
我用 5 个参数启动 exe。我检查了接收到的参数的值和长度,当 C# exe 接收到它们时,它们似乎都是正确的,但是当涉及到 DownloadFile
方法时,它似乎抛出异常。
另一方面,如果我在命令提示符中硬编码值或 运行 exe 并将参数传递给它似乎工作正常。
一旦从 C++ exe 启动它,它就会将其从堆栈跟踪写入日志,我将其打印到
username is: abc uname length is: 3| password is: abc@123 pass length is: 7| patch download from path is: .1.1.119\Backup_share\patch testing\Skype-8.81.0.268.zip FileToDownload length is: 60| patch download to path is: E:\Test\Patch.zip localDestinationFilePath length is: 17
System.Net.WebException Exception in DownloadPFile( at System.Net.WebClient.DownloadFile(Uri address, String fileName)
at System.Net.WebClient.DownloadFile(String address, String fileName) at IPMPlusAlternatePatchDownloader.IPMPlusPatchDownloader.DownloadPFile(String userName, String password, String FileToDownload, String localDestinationFilePath)
这是我正在使用的代码
static void Main(string[] args)
{
PacthDownload.DownloadPFile(args[1], args[2], args[3], args[4]);
}
private void DownloadFileShareAndFTP(string userName, string password, string FileToDownload, string localDestinationFilePath)
{
WebClient client = new WebClient();
client.Credentials = new NetworkCredential(userName, password);
client.DownloadFile(FileToDownload, localDestinationFilePath);
}
这是我对值进行硬编码而不是从参数中获取它们时的样子
static void Main(string[] args)
{
PacthDownload.DownloadFileShareAndFTP("abc", "abc@123",@"\10.1.1.119\Backup_share\patch testing\Skype-8.81.0.268.zip", @"E:\Test\Patch.zip");
//PacthDownload.DownloadPFile(args[1], args[2], args[3], args[4]);
}
private void DownloadFileShareAndFTP(string userName, string password, string FileToDownload, string localDestinationFilePath)
{
WebClient client = new WebClient();
client.Credentials = new NetworkCredential(userName, password);
client.DownloadFile(FileToDownload, localDestinationFilePath);
}
username is: abc uname length is: 3| password is: abc@123 pass length is: 7| patch download from path is: .1.1.119\Backup_share\patch testing\Skype-8.81.0.268.zip FileToDownload length is: 60| patch download to path is: E:\Test\Patch.zip localDestinationFilePath length is: 17
DownloadPFile EOF
长度是参数的长度,而不是我硬编码的值。
这是文件下载后的截图,但由于某种原因,它只是
我为此使用 visual studio 2017。 我在此处添加代码时删除了代码中的日志语句,以使其看起来更清晰。
关于如何解决这个问题有什么想法吗?
您收到的包含此路径的参数 "patch download from path is: .1.1.119\Backup_share\patch testing\Skype-8.81.0.268.zip"
似乎不包含“\”字符的字符的任何转义序列,这可能是您收到异常的原因。在每个应该修复它的“\”字符后添加一个额外的“\”。