写文件出现了问题?

来源:百度知道 编辑:UC知道 时间:2024/06/17 01:59:13
fs = new FileStream(fileEntity.FilePath, FileMode.Create, FileAccess.Write, FileShare.None);
注意这个fileEntity.FilePath,在这里有一个问题就是fileEntity.FilePathg一开始就是不存在的,我试图来创建这个文件,但是还是没有用啊,这个是什么原因
byte[] buffer = new byte[4096];
int count = 0;
while ((count = file.Read(buffer, 0, buffer.Length)) > 0)
{
fs.Write(buffer, 0, count);
}
fs.Close();
file.Close();

请参考下面的代码,文件c:\\myfile.txt不存在,文件c:\\cmd.txt存在,代码表示创建新文件c:\\myfile.txt并将文件c:\\cmd.txt中的内容写进文件c:\\myfiletxt

System.IO.FileStream fs = new System.IO.FileStream("c:\\myfile.txt", System.IO.FileMode.Create, System.IO.FileAccess.Write, System.IO.FileShare.None);
System.IO.FileStream file = new System.IO.FileStream("c:\\cmd.txt", System.IO.FileMode .Open , System.IO.FileAccess.ReadWrite, System.IO.FileShare.None);
byte[] buffer = new byte[4096];
int count = 0;
while ((count = file.Read(buffer, 0, buffer.Length)) > 0)
{
fs.Write(buffer, 0, count);
}
fs.Close();
file.Close();

判断文件路径是否有效,如果无效则建立目录