You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用C#读取含Twitter账号的TXT文件并按':'拆分ID与密码

嘿,这个需求很清晰,我来帮你搞定用C#读取并拆分这些Twitter账号的逻辑。咱们一步步来:

核心思路拆解

你的TXT文件格式是用空格分隔多个id:password条目,所以核心步骤就是:

  1. 读取整个文件的内容
  2. 把内容拆分成单个的账号条目
  3. 对每个条目按冒号分割,提取ID和密码
完整代码实现

下面是可以直接用的代码示例,我还加了格式校验和异常处理,让程序更健壮:

using System;
using System.IO;

class TwitterAccountProcessor
{
    static void Main()
    {
        // 替换成你的实际文件路径
        string accountFilePath = @"C:\Users\YourName\Documents\twitter-accounts.txt";

        try
        {
            // 读取文件全部内容
            string rawContent = File.ReadAllText(accountFilePath);

            // 按空格分割成单个账号条目,自动去除空条目(处理多余空格/换行)
            string[] accountEntries = rawContent.Split(
                new[] {' ', '\n', '\r'}, 
                StringSplitOptions.RemoveEmptyEntries
            );

            // 遍历每个账号条目,拆分ID和密码
            foreach (string entry in accountEntries)
            {
                string[] accountParts = entry.Split(':');
                
                // 校验格式是否正确:必须是ID:Password的结构
                if (accountParts.Length == 2)
                {
                    string userId = accountParts[0].Trim();
                    string password = accountParts[1].Trim();
                    
                    // 这里就可以把ID和密码传给你的Selenium修改密码逻辑了
                    Console.WriteLine($"处理账号:ID = {userId}, 密码 = {password}");
                }
                else
                {
                    Console.WriteLine($"⚠️ 无效的账号格式,跳过:{entry}");
                }
            }
        }
        catch (FileNotFoundException)
        {
            Console.WriteLine("❌ 找不到指定的账号文件,请检查路径是否正确");
        }
        catch (UnauthorizedAccessException)
        {
            Console.WriteLine("❌ 没有读取该文件的权限,请检查文件权限设置");
        }
        catch (Exception ex)
        {
            Console.WriteLine($"❌ 程序出错:{ex.Message}");
        }
    }
}
关键细节说明
  • 路径处理:记得把accountFilePath替换成你真实的文件路径,用@前缀可以避免转义反斜杠的麻烦
  • 鲁棒性优化:分割时加入了\n和\r,兼容文件里有换行的情况;用Trim()去除ID/密码前后的多余空格
  • 格式校验:判断accountParts.Length == 2,避免格式错误的条目导致程序崩溃
  • 异常处理:针对常见的文件读取异常做了单独捕获,错误提示更友好

内容的提问来源于stack exchange,提问作者Umut Pınarbaşı

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 08:51:20