创建了目录,但未在服务器上创建文件,本地运行正常

时间:2012-05-22 04:40:08

标签: c# file-io permissions

以下代码的问题是在服务器上创建了目录但是没有创建文件,当我在我的系统上本地运行然后运行正常时,请建议可以做什么,这是我的优先级1. thnx

public static void CreateLog(string strToLog)
    {
        try
        {
            string excepfile = "CacheServerLogs_" + DateTime.Now.ToShortDateString().Replace("-", "_") + ".txt";
            FileStream fs = null;
            StreamWriter sw = null;
            string mydocpath = "C:\\CacheServerLogs";
            if (!Directory.Exists(mydocpath))
                Directory.CreateDirectory(mydocpath);
            string exceptionfile = Path.Combine(mydocpath, excepfile);
            //string exceptionfile = mydocpath + "\\" + excepfile;
            if (!File.Exists(exceptionfile))
                fs = File.Create(exceptionfile);
            else
                sw = File.AppendText(exceptionfile);
            sw = sw == null ? new StreamWriter(fs) : sw;
            sw.WriteLine(DateTime.Now.ToString() + " - " + strToLog);
            sw.Close();
            if (fs != null)
                fs.Close();
        }
        catch (Exception ex)
        {
            HandleException(ex);
        }
    }

可能存在用户权限问题,但我正在尝试...给予文件夹每个可能的权限...

3 个答案:

答案 0 :(得分:0)

尝试使用 File.CreateText或File.Write

答案 1 :(得分:0)

由于您显然是在尝试写入日志文件,我通常建议您使用“标准”库来执行此操作,例如log4netNLog

答案 2 :(得分:0)

实际上错误是

string excepfile = "CacheServerLogs_" + DateTime.Now.ToShortDateString().Replace("-", "_") + ".txt";

在我的系统中,格式是22-05-2012,但在服务器上是22 \ 05 \ 2012,所以这是在找不到路径上创建错误...