windows mobile创建文本文件并用word打开
起初准备新建个word文档,但mobile无法打开,最后换成txt搞定:
运用文件读写流创建并写入内容:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
/// <summary>
/// 生成word文件
/// </summary>
/// <param name="fileName">文件路径</param>
/// <param name="conCon">内容</param>
/// <returns>word文件路径</returns>
public static string CreateDocFile(string conCon)
{
//获取系统路径
string localFilePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase) + @"\file";
if (!Directory.Exists(localFilePath))//如果文件夹不存在
{
Directory.CreateDirectory(localFilePath);
}
//文件名
string filepath = localFilePath + @"\mobileofficegzap.txt";
conCon = conCon.ToLower();
//字符转化
conCon = htmltotxt(conCon);
if (!File.Exists(filepath))//如果文件不存在
File.Create(filepath);
//创建写入文件流
StreamWriter swr = new StreamWriter(filepath, false, Encoding.Default);
//写入内容
swr.Write(conCon.ToString());
swr.Close();
//返回文件路径
return filepath;
}
/// 生成word文件
/// </summary>
/// <param name="fileName">文件路径</param>
/// <param name="conCon">内容</param>
/// <returns>word文件路径</returns>
public static string CreateDocFile(string conCon)
{
//获取系统路径
string localFilePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase) + @"\file";
if (!Directory.Exists(localFilePath))//如果文件夹不存在
{
Directory.CreateDirectory(localFilePath);
}
//文件名
string filepath = localFilePath + @"\mobileofficegzap.txt";
conCon = conCon.ToLower();
//字符转化
conCon = htmltotxt(conCon);
if (!File.Exists(filepath))//如果文件不存在
File.Create(filepath);
//创建写入文件流
StreamWriter swr = new StreamWriter(filepath, false, Encoding.Default);
//写入内容
swr.Write(conCon.ToString());
swr.Close();
//返回文件路径
return filepath;
}
调用线程打开文件:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
//生成文本文件并返回路径
string filepath = FileParse.CreateDocFile(str);
if (File.Exists(filepath))//如果文件存在开启word线程打开
{
FileParse.myFileOpen(filepath);
}
else
{
MsgManager.Show("0014");
}
string filepath = FileParse.CreateDocFile(str);
if (File.Exists(filepath))//如果文件存在开启word线程打开
{
FileParse.myFileOpen(filepath);
}
else
{
MsgManager.Show("0014");
}
myFileOpen方法:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
/// <summary>
/// 打开文档
/// </summary>
/// <param name="filePathName"></param>
public static void myFileOpen(string filePathName)
{
string exe = "";
string open = "";
try
{
string fileTag = filePathName.Substring(filePathName.LastIndexOf(".") + 1, filePathName.Length - filePathName.LastIndexOf(".") - 1);
switch (fileTag.ToLower())
{
case "doc":
{
exe = "pword.exe";
open = "-opendoc";
break;
}
case "txt":
{
exe = "pword.exe";
open = "-opendoc";
break;
}
case "xls":
{
exe = "pxl.exe";
open = "-opendoc";
break;
}
default:
break;
}
if (exe != "")
{
System.Diagnostics.Process.Start(exe, open + @filePathName);
}
else
{
System.Diagnostics.Process process = new System.Diagnostics.Process();
process.StartInfo.FileName = filePathName;
process.EnableRaisingEvents = true;
process.Start();
listProcess.Add(process);
}
}
catch
{
throw new MobileException.MobException("50002");
}
}
/// 打开文档
/// </summary>
/// <param name="filePathName"></param>
public static void myFileOpen(string filePathName)
{
string exe = "";
string open = "";
try
{
string fileTag = filePathName.Substring(filePathName.LastIndexOf(".") + 1, filePathName.Length - filePathName.LastIndexOf(".") - 1);
switch (fileTag.ToLower())
{
case "doc":
{
exe = "pword.exe";
open = "-opendoc";
break;
}
case "txt":
{
exe = "pword.exe";
open = "-opendoc";
break;
}
case "xls":
{
exe = "pxl.exe";
open = "-opendoc";
break;
}
default:
break;
}
if (exe != "")
{
System.Diagnostics.Process.Start(exe, open + @filePathName);
}
else
{
System.Diagnostics.Process process = new System.Diagnostics.Process();
process.StartInfo.FileName = filePathName;
process.EnableRaisingEvents = true;
process.Start();
listProcess.Add(process);
}
}
catch
{
throw new MobileException.MobException("50002");
}
}