C# 创建文本文件并将其保存到服务器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/922282/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Creating and saving a text file to the server
提问by RichAA
In C# ASP.Net, I would like to create and save a text file to a server. This will be happening daily (by a user action, not scheduled).
在 C# ASP.Net 中,我想创建一个文本文件并将其保存到服务器。这将每天发生(通过用户操作,而不是计划)。
I would like the location to not be in the application path but in a separate folder (for this question, lets say the folder is off the root).
我希望该位置不在应用程序路径中,而是在一个单独的文件夹中(对于这个问题,假设该文件夹不在根目录中)。
I am new to this site and if this question is too "open", feel free to let me know.
我是这个网站的新手,如果这个问题太“开放”,请随时告诉我。
Thanks for your assistance.
谢谢你的协助。
采纳答案by R Ubben
I agree with Dan Herbert. Put the path in web.config and make sure the permissions for the folder are correct.
我同意丹赫伯特的观点。将路径放在 web.config 中并确保文件夹的权限正确。
Also, make sure that path is not on the C drive. That way, if something goes wrong, or if the site is attacked, the c drive won't fill up and crash the server.
另外,请确保该路径不在 C 驱动器上。这样,如果出现问题,或者站点受到攻击,c 驱动器将不会填满并导致服务器崩溃。
Be careful with the permissions; even a simple text file can be dangerous if a hacker can muck with the path somehow. Think about someone overwriting the server's hosts file, for example.
小心权限;如果黑客可以以某种方式破坏路径,即使是简单的文本文件也可能是危险的。例如,考虑有人覆盖服务器的主机文件。
回答by Andrew Hare
Here is an example:
下面是一个例子:
// Pass a path and filename to the StreamWriter's
// constructor. The path must already exist but the
// file will be created if it does not already exist.
using (TextWriter tw = new StreamWriter("c:\foo\bar.txt"))
{
tw.WriteLine("hello world");
}
回答by Mehrdad Afshari
I don't see any specific issue in doing so. You just need to make sure the user running ASP.NET is granted the required permissions to write to the output folder.
我认为这样做没有任何具体问题。您只需要确保运行 ASP.NET 的用户被授予写入输出文件夹所需的权限。
回答by Arjan Einbu
You can use System.IO classes to save to a file on the local filesystem.
您可以使用 System.IO 类保存到本地文件系统上的文件。
using(var w = new StreamWriter(filename))
{
w.WriteLine("Hello world!");
}
回答by Joshua Belden
You'll want to use Server.MapPath to get to the physical path of your folder.
您需要使用 Server.MapPath 来获取文件夹的物理路径。
using (TextWriter tw = new StreamWriter(Server.MapPath("Folder1")))
{
tw.WriteLine("hello world");
}
回答by Darin Dimitrov
Just to add on what others have said, when writing to a file in a multi-threaded application, you need to synchronize the access to this resource. You could use ReaderWriterLockSlimto achieve this:
补充一下其他人所说的,在多线程应用程序中写入文件时,您需要同步对该资源的访问。您可以使用ReaderWriterLockSlim来实现这一点:
// Make this a static field
ReaderWriterLockSlim _lock = new ReaderWriterLockSlim();
_lock.EnterWriteLock();
try
{
File.WriteAllText(@"c:\test.txt", "some info to write");
}
finally
{
_lock.ExitWriteLock();
}
回答by Dan Herbert
One good practice to follow is to use a web.config
app setting key to define the output path of your application.
要遵循的一种好做法是使用web.config
应用程序设置键来定义应用程序的输出路径。
You'd use the ConfigurationManager.AppSettings
class for retrieving values from a web.config
. You can read how to do this here:
您将使用ConfigurationManager.AppSettings
该类从web.config
. 您可以在此处阅读如何执行此操作:
http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.appsettings.aspx
http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.appsettings.aspx