如何修复 System.Net.WebException: '文件名、目录名或卷标语法不正确

How do I fix System.Net.WebException: 'The filename, directory name, or volume label syntax is incorrect

我正在尝试为程序创建引导程序,但错误 "System.Net.WebException: 'The filename, directory name, or volume label syntax is incorrect " 不断弹出。

我一直卡在这个问题上,一直在猜测如何解决这个问题。

String pname = "Fredysploit_v2";
String dlink = "https://pastebin.com/V5NcE09n";
string title = @"Title ";
Console.Title = pname + " Bootstrapper";

Console.ForegroundColor = ConsoleColor.Blue;
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine(title);
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Downloading new Files...");
WebClient wc = new WebClient();
string key = wc.DownloadString(dlink);
string path = @"Update\" + pname + ".exe";
System.Net.WebClient Dow = new WebClient();
string patch = (@"Update");
Directory.CreateDirectory(patch);
//My problem ↓
Dow.DownloadFile(key, path);
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine(pname + " Downloaded | Updated!");
Console.WriteLine($"Now open " + patch + " and Run " + pname + ".exe");
Console.ReadKey();

我预计结果是从 pastebin 上 link 的文本下载文件,但实际结果是 "System.Net.WebException: 'The filename, directory name, or volume label syntax is incorrect"。

我认为发生的情况是您尝试将整个网站下载为一个文件,而不是下载 link 在您的 pastebin 内容 (proof here) 中编辑的文件。

也许有一种获取 link 的方法,例如:解析网站的 html 并从特定的 html 标签读取内容,或者如果存在,使用pastebin api(我现在会检查那个。There is a pastebin api。也许看看这个)


由于 api 对您帮助不大,这里有一些代码可以从网站解析 HTML: WebClient client = new WebClient(); 字符串 html代码 = client.DownloadString("<a href="https://pastebin.com/raw/dnfHuGAK" rel="nofollow noreferrer">https://pastebin.com/raw/dnfHuGAK</a>");

这允许您获取粘贴中的字符串。


这将是您对代码的最后更改,应该可以解决您的问题

    String pname = "Fredysploit_v2";
    String dlink = "https://pastebin.com/raw/dnfHuGAK"; 
    //^ changed the download link to the raw paste so you can extract your link
    string title = @"Title ";
    Console.Title = pname + " Bootstrapper";

    Console.ForegroundColor = ConsoleColor.Blue;
    Console.ForegroundColor = ConsoleColor.Cyan;
    Console.WriteLine(title);
    Console.ForegroundColor = ConsoleColor.Green;
    Console.WriteLine("Downloading new Files...");
    WebClient wc = new WebClient();
    string key = wc.DownloadString(dlink);
    string path = @"Update\" + pname + ".exe";
    System.Net.WebClient Dow = new WebClient();
    string patch = (@"Update");
    Directory.CreateDirectory(patch);
    Dow.DownloadFile(key, path);
    Console.ForegroundColor = ConsoleColor.Green;
    Console.WriteLine(pname + " Downloaded | Updated!");
    Console.WriteLine($"Now open { patch } and Run { pname }.exe");
    //^ since you are using String.Format(), make use of it :)
    Console.ReadKey();

问题是您得到的是 HTML 表单 pastebin 的整个页面,而不是您要查找的 URL。正如@Jon Skeet 在评论中提到的那样,尝试使用 https://pastebin.com/raw/V5NcE09n 作为 dlink 所以它显示如下:

String dlink = "https://pastebin.com/raw/V5NcE09n";

更新后的代码解决方案如下:

String pname = "Fredysploit_v2";
String dlink = "https://pastebin.com/raw/V5NcE09n";
string title = @"Title ";
Console.Title = pname + " Bootstrapper";

Console.ForegroundColor = ConsoleColor.Blue;
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine(title);
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Downloading new Files...");
WebClient wc = new WebClient();
string key = wc.DownloadString(dlink);
string path = @"Update\" + pname + ".exe";
System.Net.WebClient Dow = new WebClient();
string patch = (@"Update");
Directory.CreateDirectory(patch);

Dow.DownloadFile(key, path);
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine(pname + " Downloaded | Updated!");
Console.WriteLine($"Now open " + patch + " and Run " + pname + ".exe");
Console.ReadKey();