简体   繁体   English

创建文本文件并将其保存到服务器

[英]Creating and saving a text file to the server

In C# ASP.Net, I would like to create and save a text file to a server. 在C#ASP.Net中,我想创建一个文本文件并将其保存到服务器。 This will be happening daily (by a user action, not scheduled). 这将每天发生(通过用户操作而不是预定的时间)。

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. 谢谢你的协助。

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. 另外,请确保该路径不在C驱动器上。 That way, if something goes wrong, or if the site is attacked, the c drive won't fill up and crash the server. 这样,如果出现问题或站点受到攻击,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. 例如,考虑有人覆盖服务器的主机文件。

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 . 您将使用ConfigurationManager.AppSettings类从web.config检索值。 You can read how to do this here: 您可以在此处阅读如何执行此操作:

http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.appsettings.aspx http://msdn.microsoft.com/zh-CN/library/system.configuration.configurationmanager.appsettings.aspx

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");
}

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!");
}

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 ReaderWriterLockSlim to 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();
}

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的用户所需的权限即可写入输出文件夹。

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"); 
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM