Hello,
I have a solution with a web app and multiple class libraries - this solution was created in Visual studio 2005 but was recently converted to a VS 2008 application. Since I've done this, I can no longer perform any method or operation within the System.IO namespace - I get the following SecurityException:
{"Request for the permission of type 'System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed."}
Now, if I create a blank solution in 2005 (Which I still have installed) - I can use Directory.Create or File.Create just by adding Network service to the ACL for the folder I want to write to, and it doesn't cause any problems at all.
I have searched on the net for this and nothing has been useful, or worked. I have tried creating a FileIOPermission type and asserting access to the folder, but I don't really know how this works.
How can I make System.IO's static methods and classes work again in Visual Studio 2008? Have I done/not done something that I needed to do when I installed 2008 and .NET framework 3/3.5? - Am I trying to use version 2.0 assemblies when I shouldn't be?
Any help greatly appreciated....
Matt
Guest UserPosted Jan 14, 2008, 4:31 PM
using System;
using System.IO;
class Test
{
public static void Main()
{
string path = @"c:\temp\MyTest.txt";
if (!File.Exists(path))
{
// Create a file to write to.
using (StreamWriter sw = File.CreateText(path))
{
sw.WriteLine("Hello");
sw.WriteLine("And");
sw.WriteLine("Welcome");
}
}
// Open the file to read from.
using (StreamReader sr = File.OpenText(path))
{
string s = "";
while ((s = sr.ReadLine()) != null)
{
Console.WriteLine(s);
}
}
try
{
string path2 = path + "temp";
// Ensure that the target does not exist.
File.Delete(path2);
// Copy the file.
File.Copy(path, path2);
Console.WriteLine("{0} was copied to {1}.", path, path2);
// Delete the newly created file.
File.Delete(path2);
Console.WriteLine("{0} was successfully deleted.", path2);
}
catch (Exception e)
{
Console.WriteLine("The process failed: {0}", e.ToString());
}
}
}
Is this code doing anything dramatically different from your code? Since you're trying to create directories from a web app, I would look into the permissions of the directories amd files you're accessing... does the user account have the necessary access priveleges?
mattPosted Jan 14, 2008, 6:45 AM
I should add that this is an application running on a local machine, not a shared server - there are no UNC paths or any other external resources.