C# 创建文件时发生UnauthorizedAccessException
我有这段代码,它从ftp服务器下载文件,然后在设置的路径中创建该文件C# 创建文件时发生UnauthorizedAccessException,c#,C#,我有这段代码,它从ftp服务器下载文件,然后在设置的路径中创建该文件 string inputfilepath = @"C:\Users\me\Documents\Test"; string ftphost = "ftp_server"; string ftpfilepath = @"/TestDOTNET/text.TXT"; string ftpfullpath = "ftp://" + ftphost + ftpfilepath;
string inputfilepath = @"C:\Users\me\Documents\Test";
string ftphost = "ftp_server";
string ftpfilepath = @"/TestDOTNET/text.TXT";
string ftpfullpath = "ftp://" + ftphost + ftpfilepath;
using (WebClient request = new WebClient())
{
request.Proxy = null;
request.Credentials = new NetworkCredential("user", "pass");
byte[] fileData = request.DownloadData(ftpfullpath);
File.SetAttributes(inputfilepath, FileAttributes.Normal);
using (FileStream file = File.Create(inputfilepath)) // this is the line where I get the exception
{
file.Write(fileData, 0, fileData.Length);
file.Close();
}
MessageBox.Show("Download Complete");
}
我试图创建app.manifest并将requestedExcetuion级别设置为requireAdministrator
,但仍然没有得到任何更改
感谢您抽出时间您确定运行应用程序的用户对文件系统具有写访问权限吗?您确定运行应用程序的用户对文件系统具有写访问权限吗?您应该检查运行应用程序的有效用户-通常是不同的用户(例如网络服务用户)来自你自己-拥有适当的权利
- 检查IIS应用程序池设置,该设置是运行应用程序的用户
- 为目标文件夹上的此类用户分配适当的权限
- 检查IIS应用程序池设置,该设置是运行应用程序的用户
- 为目标文件夹上的此类用户分配适当的权限
只读属性。如果没有,则创建目录,然后创建test.txt
以写入其中。
例::
您应该首先测试目录路径是否存在,如果存在,则否定该目录的只读属性。如果没有,则创建目录,然后创建test.txt
以写入其中。
例::
我认为这应该作为评论提出来,但是。在这个文件夹中,我可以创建、编辑、复制我想要的任何东西。我想我有权写信给这个文件夹。我想这应该作为评论提出来,但是。在这个文件夹中,我可以创建、编辑、复制我想要的任何东西。我想我有写入此文件夹的所有权限。我想你的“inputfilepath”是一个显然是只读的目录。@Abhineet你说得太对了!我以只读方式取消设置文件夹的属性,但当我关闭并重新打开“属性”窗口时,它仍然是只读的。我怎样才能取消设置呢?是通过编程还是手动?我想这没关系,但通过编程会更好。var di=new DirectoryInfo(“inputfilepath”);di.Attributes&=~FileAttributes.ReadOnly;我认为你的“inputfilepath”是一个显然是只读的目录。@Abhineet你说得太对了!我以只读方式取消设置文件夹的属性,但当我关闭并重新打开“属性”窗口时,它仍然是只读的。我怎样才能取消设置呢?是通过编程还是手动?我想这没关系,但通过编程会更好。var di=new DirectoryInfo(“inputfilepath”);di.Attributes&=~FileAttributes.ReadOnly;
string inputdirpath = @"C:\Users\me\Documents\Test";
string inputfilepath = inputdirpath + "\text.TXT";
// Downloading Stuff
if(Directory.Exists(inputdirpath ))
{
var di = new DirectoryInfo("inputfilepath ");
di.Attributes &= ~FileAttributes.ReadOnly;
using (FileStream file = File.Create(inputfilepath))
{
file.Write(fileData, 0, fileData.Length);
file.Close();
}
MessageBox.Show("Download Complete");
}
else
{
// Create Directory
// Set Attributes
// Create file
// Write data
}