如何在 Windows 中的 WCF 服务应用程序中创建文件 [英] How to create a file in WCF service application in windows
本文介绍了如何在 Windows 中的 WCF 服务应用程序中创建文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在开发 WCF 服务应用程序.我想在我的一个函数中创建一个文件所以这次我就是这样做的.首先我去目录创建一个文件然后我读/写.
I'm working on WCF services application. I want to create a file in one of my function so now this time I'm doing like this. First I go to directory creates a file then i do read/write.
string path = AppDomain.CurrentDomain.BaseDirectory;
path += "Emp_data\\json_data.json";
StreamReader reader = new StreamReader(path);
StreamWriter writer = new StreamWriter(path);
我知道我这样做是错误的.请建议我一个更好的方法,以便如果没有文件和文件夹它会自动创建.
I know I'm doing this in wrong way. Please suggest me a better way so that if there in no file and folder It will create automatically.
推荐答案
在 WCF 中创建文件不会发生任何额外的事情,因此您可以这样做
Nothing extra happens with creating a file in WCF so you can do that like this
string path = AppDomain.CurrentDomain.BaseDirectory;
String dir = Path.GetDirectoryName(path);
dir += "\\Emp_data";
string filename = dir+"\\Json_data.json";
if (!Directory.Exists(dir))
Directory.CreateDirectory(dir); // inside the if statement
FileStream fs = File.Open(filename,FileMode.OpenOrCreate, FileAccess.ReadWrite);
StreamReader reader = new StreamReader(fs);
这篇关于如何在 Windows 中的 WCF 服务应用程序中创建文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文