问题描述
我试图写一个代码,这样我就可以记录错误消息。我想用命名的日期文件,并希望每一天创建新的日志文件。通过稍微留意一下周围发生后,我带着下面的代码...
I was trying to write a code so that I could log the error messages. I am trying to name the file with the date and would like to create a new log file for each day. After going through a little look around, I came with the following code...
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(文件路径,FileAttributes.Normal)并没有帮助也没有了RemoveReadOnlyAccess(包括在上面的代码)。当我检查了文件夹的属性,它只读标记,但即使我剔掉这回又来了。我检查了文件夹的权限和除特别许可,这点我是不能够改变,一切都被允许。
任何建议,我应该如何进行将不胜感激。
的链接讨论了类似的问题,但我不能让我的事与上市有建议的工作。
I found a forum that has discussed about a similar problem but using File.SetAttrributes(filePath, FileAttributes.Normal) did not help neither did the RemoveReadOnlyAccess (included in the code above). When I check the properties of the folder, it has read only marked but even when I tick that off it comes back again. I checked the permissions on the folder and except for the special permission, which I was not able to change, everything is allowed. Any suggestion on how I should proceed would be appreciated. Why is access to the path denied? the link discusses about a similar problem, but I wasn't able to get my thing working with suggestions listed there.
感谢您抽出时间来看看这个。
Thanks for taking time to look at this.
推荐答案
您路径是奇怪的:我的文档目录必须是C:\Users\MyName\Documents\
Your path is strange : "My documents" directory must be "C:\Users\MyName\Documents\"
您可以以使用环境中轻松地纠正:
You can use Environment in order to correct it easily :
String myDocumentPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
请注意,它会查看在我的文档的运行你的EXE用户的文件夹中。
Note that it will acces to "My documents" folder of the user that running your exe.
二错误,CreateDirectory必须在参数的路径,而不是文件。使用像你这样会创建一个子目录的文件名。 !所以你不能创建这个名称的文件
Second error, CreateDirectory must have a path in argument, not a file. using like you do will create a sub-directory with the file name. So you can't create a file with this name !
试试这个:
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);
[...]
}
}
这篇关于System.UnauthorizedAccessException的同时创建文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!