帮助文件IO类 [英] Help With File IO Class

查看:53
本文介绍了帮助文件IO类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!
请帮助我将信息写入文本文件.我已经在IIS上运行的ASP.NET应用程序的根目录中创建了一个文本文件.我想在单击提交"按钮后将表单的值写入此文本文件,例如名字,姓氏和日期.当我不在IIS上运行文本应用程序时,即使用ASP.NET Web DevelopmentServer运行该文件时,该应用程序将写入该文本文件,但在IIS上部署该文本文件后,它将不再写入该文本文件.这是我的代码:

Hi!
Please help me writing information to a text file. I have created a text file in the root of my ASP.NET application running on IIS. I want to write the value of a form into this text file, like first name, last name and date after the click of the submit button. The application writes to the text file when I''m not running it on IIS, that is when running it using ASP.NET Web DevelopmentServer, but after deploying it on IIS it doesn''t write to the text file anymore. Here is my code:

String filePath = Server.MapPath("~/Data.txt");
String content = String.Format("firstname = {0}, lastname {1}",txtFirstName.Text, txtLastName.Text);
System.IO.File.WriteAllText(filePath, content);


当不在IIS上运行时,它可以很好地工作,请任何人都可以帮助我.

谢谢!


It work well when not running on IIS, please can anyone help me.

Thanks!

推荐答案

类似的声音可能是权限问题.在此处 [ ^ ].

您是否获得异常还是只是被忽略了?您是否设置了断点并运行代码以查看是否发生了什么?
Sounds like it might possibly be a permissions issue. Start here[^].

Do you get an exception or does it simply get ignored? Have you set a breakpoint and run through the code to see if what is going on?


除了Mark在其解决方案中关于访问权限问题的内容外,您的方法还存在严重的设计缺陷.由于您使用的是 System.IO.File.WriteAllText [ ^ ],文件将在每次写请求时都会被覆盖,这听起来不太有用.如果您使用其他方法(例如,打开,写入和关闭文件来保留先前的条目),则会遇到并发问题,因为分开的请求可能会导致对该文件的写入,并且您必须确保妥善处理这些文件.

为了避免麻烦,为什么不使用log4Net这样的日志框架.这是从 log4J [ ^ ],可以在以下位置找到:什么是Apache log4net™ [^ ].

问候,

曼弗雷德(Manfred)
Besides what Mark said in his solution regarding the access rights issues there is a serious design flaw in your approach. Since you are using System.IO.File.WriteAllText[^] the file will be overwritten upon each write request which does not sound very useful. Should you use another approach like opening, writing and closing the file to preserve previous entries you''ll run into concurrency issues as to separate requests might cause a writing to said file and you''ll have to make sure to handle these well.

To save yourself the trouble why not use a logging framework like log4Net. It''s a spinoff from log4J[^] and can be found here: What is Apache log4net™[^].

Regards,

Manfred


这篇关于帮助文件IO类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆