6

我正在尝试编写代码以便记录错误消息。我正在尝试用日期命名文件,并希望为每一天创建一个新的日志文件。经过一番环顾后,我得到了以下代码......

class ErrorLog
{
    public void WriteErrorToFile(string error)
    {
        //http://msdn.microsoft.com/en-us/library/aa326721.aspx refer for more info
        string fileName = DateTime.Now.ToString("dd-MM-yy", DateTimeFormatInfo.InvariantInfo);

        //@ symbol helps to ignore that escape sequence thing
        string filePath = @"c:\users\MyName\mydocuments\visual studio 2012\projects\training\" +
                            @"discussionboard\ErrorLog\" + fileName + ".txt";

        if (File.Exists(filePath))
        {
           // File.SetAttributes(filePath, FileAttributes.Normal);
            File.WriteAllText(filePath, error);
        }
        else
        {
            Directory.CreateDirectory(filePath);
           // File.SetAttributes(filePath, FileAttributes.Normal)
           //Throws unauthorized access exception
            RemoveReadOnlyAccess(filePath);
            File.WriteAllText(filePath, error);
        }
    }

    public static void RemoveReadOnlyAccess(string pathToFile)
    {
        FileInfo myFileInfo = new FileInfo(pathToFile);
        myFileInfo.IsReadOnly = false;
        myFileInfo.Refresh();
    }

    /*Exception thrown:
     * UnAuthorizedAccessException was unhandled.
     * Access to the path 'c:\users\anish\mydocuments\visual studio 2012\
     * projects\training\discussionboard\ErrorLog\04\12\2013.txt' is denied.
     */
}

我发现一个讨论过类似问题的论坛,但使用 File.SetAttrributes(filePath, FileAttributes.Normal) 并没有帮助 RemoveReadOnlyAccess (包含在上面的代码中)。当我检查文件夹的属性时,它已标记为只读,但即使我勾选它,它也会再次出现。我检查了文件夹的权限,除了我无法更改的特殊权限外,一切都是允许的。任何关于我应该如何进行的建议将不胜感激。 为什么访问路径被拒绝?该链接讨论了类似的问题,但我无法使用那里列出的建议来解决问题。

感谢您花时间看这个。

4

4 回答 4

6

您的路径很奇怪:“我的文档”目录必须是“C:\Users\MyName\Documents\”

您可以使用 Environment 来轻松纠正它:

String myDocumentPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

请注意,它将访问运行您的 exe 的用户的“我的文档”文件夹。

第二个错误,CreateDirectory 在参数中必须有路径,而不是文件。像你一样使用将创建一个带有文件名的子目录。所以你不能用这个名字创建一个文件!

尝试这个 :

String fileName = DateTime.Now.ToString("d", DateTimeFormatInfo.InvariantInfo);

String filePath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
    + @"\visual studio 2012\projects\training\discussionboard\ErrorLog\";
String fileFullName = filePath + fileName + ".txt";

    if (File.Exists(fileFullName ))
    {
        File.WriteAllText(fileFullName , error);
    }
    else
    {
        Directory.CreateDirectory(filePath);

[...]
    }
}
于 2013-04-12T15:06:25.470 回答
3

一些可能的原因:

  • 您的应用程序未在允许访问该路径/文件的帐户下运行
  • 该文件被其他进程锁定以进行写入(或者也可能读取)

第一种情况可以通过检查进程在哪个帐户下运行并验证该帐户是否具有适当的权限来解决。

另一种情况可以通过检查是否有任何其他进程正在锁定文件来解决(例如,使用“WhosLocking”或“ProcessExplorer”等工具

于 2013-04-12T15:03:59.057 回答
1

我必须以管理员身份运行我的应用程序才能写入 c: 中的受保护文件夹。例如,如果在 Visual Studio 中调试您的应用程序,请确保右键单击“C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\devenv.exe”并选择“以管理员身份运行”。然后从那里打开您的解决方案。我的应用程序试图写入 c:\

于 2015-03-27T21:02:45.227 回答
1

检查您的防病毒软件,它可能会阻止文件创建。

于 2021-03-24T20:30:57.870 回答