读写文本

本文提供了多种文本文件读写的方法,包括在不同路径下创建、读取和写入文本文件的具体实现,适用于WinCE等环境。
部署运行你感兴趣的模型镜像
最近做的项目用到了很多读写文本的东西,在这里把它复制上来备用,有的也是从网上拿来的嘿嘿:

void outputHtml(string content,string link , StreamWriter sw,string filePathDir)
{
StreamWriter sw;
if (File.Exists(@"D:\test\333.html"))
{
File.Delete(@"D:\test\333.html");

}

sw = File.CreateText(@"D:\test\333.html");

sw.WriteLine("<p><span class=\"STYLE9\">"+link+"</span><br />");
sw.WriteLine("<p><span class=\"STYLE10\">"+content+"</span><br />");
sw.WriteLine("<p><span class=\"STYLE11\">" + "保存路径: "+filePathDir + "</span></p>");


sw.Close();

}

private void button1_Click(object sender, EventArgs e)
{

string path = "Program Files\\TestWinCE\\configPIO.txt";
using (StreamReader sr = File.OpenText(path))
{
string s = " ";
s = sr.ReadLine();
textBox1.Text = s;
sr.Close();
}

}


//测试写入
private void outputHtml()
{
StreamWriter sw;
if (File.Exists("\\Windows\\333.txt"))
{
File.Delete("\\Windows\\333.txt");

}


sw = File.CreateText("\\Windows\\333.txt");
sw.WriteLine("sssss");

sw.WriteLine("vdfg");

sw.Close();

}

http://blog.163.com/jp317jay@126/blog/static/8859828520097505624947/ 读写文本

private void writeTxt(string fullName)
{
StreamWriter sr;
if (File.Exists(fullName)) //如果文件存在,则创建File.AppendText对象

{
sr = File.AppendText(FILE_NAME);
}
else //如果文件不存在,则创建File.CreateText对象
{
sr = File.CreateText(fullName);
}
sr.WriteLine(str);
sr.Close();
}

string apppath = Assembly.GetExecutingAssembly().GetName().CodeBase;
apppath = Path.GetDirectoryName(apppath);
string ss= Path.Combine(apppath, "address.txt");
MessageBox.Show(ss);

判断combox 添加选项两种方法
String sURL = Convert.ToString(e.Url);
bool IsAdd = false;
for (int i = 0; i < this.txturl.Items.Count; i++)
{
if (sURL.Equals((String)this.txturl.Items[i]))
{ IsAdd = true; break; }
}
if (!IsAdd)
{
this.txturl.Items.Add(sURL);
}

/*
int index = txturl.FindStringExact(sURL);
if (e.Url == webBrowser1.Document.Url)


if (index == -1)
{
txturl.Items.Add(sURL);
txturl.SelectedIndex = txturl.FindStringExact(sURL);
}
else
{
txturl.SelectedIndex = index;
}
*/

启动 相应程序的代码

ProcessStartInfo startInfo = new ProcessStartInfo(listFileFounded.SelectedItem.ToString(), null);

Process.Start(startInfo);


public static string GetApplicationPath()
{
System.Reflection.Module[] modules = System.Reflection.Assembly.GetExecutingAssembly().GetModules();
string aPath = System.IO.Path.GetDirectoryName(modules[0].FullyQualifiedName);
if ((aPath != "") && (aPath[aPath.Length - 1] != '\\'))
aPath += '\\';
return aPath;
}
在WinCE下,不能用相对路径,必须用绝对路径(Full Path) 来定位文件。

如,在Program Files\TestProgram目录下的abc.txt文件,只能用

Program Files\TestProgram\abc.txt来定位,而不能用abc.txt定位。

如:

private void button1_Click(object sender, EventArgs e)
{

string path = "Program Files\\TestWinCE\\configPIO.txt";
using (StreamReader sr = File.OpenText(path))
{
string s = " ";
s = sr.ReadLine();
textBox1.Text = s;
sr.Close();
}

}



与Windows NT不一样,Windows CE没有当前目录这个概念,因此,任何路径只是相对于根目录而言的。如果你的软件给文件或目录使用相对路径,那么你很可能把它们移到别的地方了。例如,路径".\abc"在Windows CE中被当作"\abc"看待。http://www.cnblogs.com/buffer/archive/2009/04/13/1434748.html
找到了,呵呵
TCHAR szFileName[256] = {0};
DWORD dwNameLen = ::GetModuleFileName(NULL,szFileName,256);
if(dwNameLen == 0)
{
// Error process
}
//szFileName is Like "\directory\filename.ex
m_Caption.SetText(szFileName);

转自网上的方法

方法1:
Directory.GetCurrentDirectory()。
这个方法只能在.NET的完整版中使用,NETCF中不支持该功能,调用时会引发异常。

方法2:
System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase)。
这个方法是MSDN中给出的针对NETCF平台的,当在PC的NET完整版中获取到的路径中测试时,发现最终的路径中带有file:前缀,如file:\c:\debug,一般情况下我们并不需要这个前缀,可以手动将其去掉。

方法3:
System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().ManifestModule.FullyQualifiedName).
该方法也是针对NETCF给出的解决方案,当在PC的NET完整版测试中可以获取到一致的结果。

小结:
如果考虑到代码在PC和PDA上的兼容性,则建议使用第三种方法。第一种方法是万万不行的,而且第一种方式获取到的路径也并不一定就是真正的应用程序所在的路径。
http://www.cnblogs.com/sail/archive/0001/01/01/1521992.html


http://blog.sina.com.cn/s/blog_4d91c1660100fpcu.html
http://hi.baidu.com/372590721/blog/item/c3c17f2b1ad40a3f5243c146.html
http://zhidao.baidu.com/question/48190857.html读写 txt文件

http://blog.youkuaiyun.com/zhzuo/archive/2004/04/05/22027.aspx

http://ncny.hljagri.gov.cn/syjs/004.htm

您可能感兴趣的与本文相关的镜像

Linly-Talker

Linly-Talker

AI应用

Linly-Talker是一款创新的数字人对话系统,它融合了最新的人工智能技术,包括大型语言模型(LLM)、自动语音识别(ASR)、文本到语音转换(TTS)和语音克隆技术

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值