c#如何读取远程TXT文档就像读取本地文档一样

wgejydy 2010-02-19 10:26:49
c#如何读取远程TXT文档就像读取本地文档一样
wuyisky.IniFiles bc = new wuyisky.IniFiles("http://www.xxxx.com//list.txt");


namespace wuyisky
{
/**/
/**/
/**/
/// <summary>
/// IniFiles的类
/// </summary>
public class IniFiles
{
public string FileName; //INI文件名
//声明读写INI文件的API函数
[DllImport("kernel32")]
private static extern bool WritePrivateProfileString(string section, string key, string val, string filePath);
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section, string key, string def, byte[] retVal, int size, string filePath);
//类的构造函数,传递INI文件名
public IniFiles(string AFileName)
{
// 判断文件是否存在
FileInfo fileInfo = new FileInfo(AFileName);
//Todo:搞清枚举的用法
if ((!fileInfo.Exists))
{ //|| (FileAttributes.Directory in fileInfo.Attributes))
//文件不存在,建立文件
System.IO.StreamWriter sw = new System.IO.StreamWriter(AFileName, false, System.Text.Encoding.Default);
try
{
sw.Write("#表格配置档案");
sw.Close();
}

catch
{
throw (new ApplicationException("Ini文件不存在"));
}
}
//必须是完全路径,不能是相对路径
FileName = fileInfo.FullName;
}
只能使用本地路径 如何使用远程路径呢?
...全文
812 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
打酱油m_88_ok 2012-01-12
  • 打赏
  • 举报
回复
有没有其他方法,不发布,不共享
jsppa 2011-11-29
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 sp1234 的回复:]

使用 System.Net.WebClient 类的 DownloadFile 或者 DownloadString 方法先下载到本地文件或者 MemoryStream,然后再读出。
[/Quote]

正解
菠萝帅帅的 2011-08-13
  • 打赏
  • 举报
回复
四库就是 现在文件到本地 然后再读数据
mjp1234airen4385 2010-02-20
  • 打赏
  • 举报
回复
如果仅仅是读取,还好。如果是修改就麻烦了。
你先把远程文件下载到本地,然后在本地操作,就好了。
不管别人给你多少种方法,
核心的思想,还是把文件下载到本地
wgejydy 2010-02-20
  • 打赏
  • 举报
回复
当然是读取了啊
。。。。。。。。。。。
wgejydy 2010-02-19
  • 打赏
  • 举报
回复
只需要读取远程TXT一个值
比如
[版本]
bbhm=20100219

我只读取远程的bbhm的值合本地的bbhm值进行比较
如果远程的这个值大于本地的值就下载进行更换本地的文件
wgejydy 2010-02-19
  • 打赏
  • 举报
回复
我想写个类 能直接读取 远程TXT文档
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Collections;
using System.Collections.Specialized;

namespace wuyisky
{
/**/
/**/
/**/
/// <summary>
/// IniFiles的类
/// </summary>
public class IniFiles
{
public string FileName; //INI文件名
//声明读写INI文件的API函数
[DllImport("kernel32")]
private static extern bool WritePrivateProfileString(string section, string key, string val, string filePath);
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section, string key, string def, byte[] retVal, int size, string filePath);
//类的构造函数,传递INI文件名
public IniFiles(string AFileName)
{
// 判断文件是否存在
FileInfo fileInfo = new FileInfo(AFileName);
//Todo:搞清枚举的用法
if ((!fileInfo.Exists))
{ //|| (FileAttributes.Directory in fileInfo.Attributes))
//文件不存在,建立文件
System.IO.StreamWriter sw = new System.IO.StreamWriter(AFileName, false, System.Text.Encoding.Default);
try
{
sw.Write("#表格配置档案");
sw.Close();
}

catch
{
throw (new ApplicationException("Ini文件不存在"));
}
}
//必须是完全路径,不能是相对路径
FileName = fileInfo.FullName;
}

//从Ini文件中,将指定的Section名称中的所有Ident添加到列表中
public void ReadSection(string Section, StringCollection Idents)
{
Byte[] Buffer = new Byte[16384];
//Idents.Clear();

int bufLen = GetPrivateProfileString(Section, null, null, Buffer, Buffer.GetUpperBound(0),
FileName);
//对Section进行解析
GetStringsFromBuffer(Buffer, bufLen, Idents);
}

private void GetStringsFromBuffer(Byte[] Buffer, int bufLen, StringCollection Strings)
{
Strings.Clear();
if (bufLen != 0)
{
int start = 0;
for (int i = 0; i < bufLen; i++)
{
if ((Buffer[i] == 0) && ((i - start) > 0))
{
String s = Encoding.GetEncoding(0).GetString(Buffer, start, i - start);
Strings.Add(s);
start = i + 1;
}
}
}
}
//从Ini文件中,读取所有的Sections的名称
public void ReadSections(StringCollection SectionList)
{
//Note:必须得用Bytes来实现,StringBuilder只能取到第一个Section
byte[] Buffer = new byte[65535];
int bufLen = 0;
bufLen = GetPrivateProfileString(null, null, null, Buffer,
Buffer.GetUpperBound(0), FileName);
GetStringsFromBuffer(Buffer, bufLen, SectionList);
}
//读取指定的Section的所有Value到列表中
public void ReadSectionValues(string Section, NameValueCollection Values)
{
StringCollection KeyList = new StringCollection();
ReadSection(Section, KeyList);
Values.Clear();
foreach (string key in KeyList)
{
Values.Add(key, ReadString(Section, key, ""));

}
}

//确保资源的释放
~IniFiles()
{
UpdateFile();
}
}
}
能达到这个效果
当然只需要读取的方法
wgejydy 2010-02-19
  • 打赏
  • 举报
回复
WebResponse result = null;
WebRequest req = WebRequest.Create("");
result = req.GetResponse();
Stream sr= result.GetResponseStream();
Encoding encode = System.Text.Encoding.GetEncoding("GB2312");
StreamReader sr = new StreamReader(sr, encode);
能实现和本地一样?
wuyq11 2010-02-19
  • 打赏
  • 举报
回复
WebResponse result = null;
WebRequest req = WebRequest.Create("");
result = req.GetResponse();
Stream sr= result.GetResponseStream();
Encoding encode = System.Text.Encoding.GetEncoding("GB2312");
StreamReader sr = new StreamReader(sr, encode);
wgejydy 2010-02-19
  • 打赏
  • 举报
回复
我想直接读取远程列表 有需要再下载的一个值
如果远程值合本地值不一样 就下载更换之
如何实现
wgejydy 2010-02-19
  • 打赏
  • 举报
回复
给个详细方法才好 网络方面 有点差
  • 打赏
  • 举报
回复
使用 System.Net.WebClient 类的 DownloadFile 或者 DownloadString 方法先下载到本地文件或者 MemoryStream,然后再读出。

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧