Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在c中创建文件和文件夹#_C#_Asp.net_.net - Fatal编程技术网

C# 在c中创建文件和文件夹#

C# 在c中创建文件和文件夹#,c#,asp.net,.net,C#,Asp.net,.net,我试图在用户文件夹中创建一个文件,并用一些基本文本填充它。看起来很简单,但我不断地得到一个错误: 找不到路径“C:\websites\admin\upload\users\testuserID\testuserIDSampleRecord.txt”的一部分“}System.Exception{System.IO.DirectoryNotFoundException} 我的网站位于:c:\websites\testsite\,因此完整路径应为: c:/websites/testsite/admin

我试图在用户文件夹中创建一个文件,并用一些基本文本填充它。看起来很简单,但我不断地得到一个错误:

找不到路径“C:\websites\admin\upload\users\testuserID\testuserIDSampleRecord.txt”的一部分“}System.Exception{System.IO.DirectoryNotFoundException}

我的网站位于:
c:\websites\testsite\
,因此完整路径应为:

c:/websites/testsite/admin/upload/users/
IIS本地主机设置为指向
c:/websites/
,因此当我运行它时,我键入
localhost/testsite
来访问它

以下是我所拥有的:

 try
            {
           string SampleCaseText =  BuildTextRecord();
           string username = (string)Session["userid"];
           string folderPath = "/testsite/admin/upload/users/" + username;
           bool IsExists = System.IO.Directory.Exists(Server.MapPath(folderPath));

            if(!IsExists)
                System.IO.Directory.CreateDirectory(Server.MapPath(folderPath));

            System.IO.File.Create(folderPath + "/" + username + "SampleRecord.txt");


            File.WriteAllText(Path.Combine(folderPath, username + "SampleRecord.txt"), SampleCaseText);


            }

它在正确的位置创建了新文件夹testuserID,但在尝试创建/写入文件时失败。

我立即发现有几个错误,可能会造成麻烦

首先,不要使用web文件夹分隔符,而是使用windows文件夹分隔符

其次,您没有在所有应该使用的位置使用
Server.MapPath
,导致使用web相对路径而不是本地windows路径

尝试类似的方法,在开始时我将文件夹转换为windows路径,并将转换后的
userFilename
放入它自己的变量中,然后使用它来代替

string folderPath = Server.MapPath("/testsite/admin/upload/users/" + username);
bool IsExists = System.IO.Directory.Exists(folderPath);
if(!IsExists)
    System.IO.Directory.CreateDirectory(folderPath);

string userFilename = Path.Combine(folderPath, username + "SampleRecord.txt");
System.IO.File.Create(userFilename);
File.WriteAllText(userFilename, SampleCaseText);

我认为有几个错误可能会引起麻烦

首先,不要使用web文件夹分隔符,而是使用windows文件夹分隔符

其次,您没有在所有应该使用的位置使用
Server.MapPath
,导致使用web相对路径而不是本地windows路径

尝试类似的方法,在开始时我将文件夹转换为windows路径,并将转换后的
userFilename
放入它自己的变量中,然后使用它来代替

string folderPath = Server.MapPath("/testsite/admin/upload/users/" + username);
bool IsExists = System.IO.Directory.Exists(folderPath);
if(!IsExists)
    System.IO.Directory.CreateDirectory(folderPath);

string userFilename = Path.Combine(folderPath, username + "SampleRecord.txt");
System.IO.File.Create(userFilename);
File.WriteAllText(userFilename, SampleCaseText);

这里有一些可能会有所帮助的东西——为了让它更容易一点,我没有包含创建文件或文件夹的代码,只是简单地抓取一个现有文件,打开它并写入它

希望这能给你一些方向,你可以从那里开始

private void Error(string error)
    {
        var dir= new DirectoryInfo(@"yourpathhere");
        var fi = new FileInfo(Path.Combine(dir.FullName, "errors.txt"));

        using (FileStream fs = fi.OpenWrite())
        {
            StreamWriter sw = new StreamWriter(fs);
            sw.Write(error);
            sw.Close();
            sw.Dispose();
        }

    }

需要注意的一点是:确保您对要写入的文件夹和文件拥有权限。

这里有一些可能会有所帮助的东西-为了让它更简单,我没有包含创建文件或文件夹的代码,只是简单地抓取一个现有文件,打开它并写入它

希望这能给你一些方向,你可以从那里开始

private void Error(string error)
    {
        var dir= new DirectoryInfo(@"yourpathhere");
        var fi = new FileInfo(Path.Combine(dir.FullName, "errors.txt"));

        using (FileStream fs = fi.OpenWrite())
        {
            StreamWriter sw = new StreamWriter(fs);
            sw.Write(error);
            sw.Close();
            sw.Dispose();
        }

    }

需要注意的一点是:确保您对要写入的文件夹和文件拥有权限。

尝试使用
System.Path.CombinePath
函数,而不是字符串串联。此外,我认为调用
System.IO.file.Create
应该使用Windows``而不是web
//code>来创建
,您发现了什么错误设置?访问权限如何?能否通过windows资源管理器检查文件夹是否已创建并进入目录?我相信根
C:
类似于
程序文件
,它需要提升权限。请尝试将目录更改为本地
我的文档
应用程序数据
,如果错误消失,则是权限。@Greg在将文件写入这些位置时,您是正确的。但是,如果您先创建一个文件夹,那么您就可以毫无问题地将文件写入该文件夹(当然,写入操作是正确的)尝试使用
System.Path.CombinePath
函数,而不是字符串连接。此外,我认为对
System.IO.File.Create
的调用应该使用Windows`\`而不是web
/
来执行。您会遇到什么错误?访问权限如何?您是否可以检查文件夹是否通过wind创建到目录中ows explorer?我相信根
C:
类似于
程序文件
,它需要提升权限。请尝试将目录更改为本地
我的文档
应用程序数据
,如果错误消失,则为权限。@Greg将文件写入这些位置是正确的。但是如果您首先创建一个文件夹,然后就可以毫无问题地写入该文件夹(当然,写入正确)谢谢!alsmot在那里。现在我得到:System.IO.IOException:进程无法访问文件'C:\websites\testwebsite\admin\upload\users\testUser\TestUserSampleCord.txt',因为它正被另一个进程使用。谢谢!alsmot在那里。现在我得到:System.IO.IOException:进程无法访问文件'C:\websites\testwebsite\admin\u'pload\users\testUser\testusersamplecord.txt',因为另一个进程正在使用它。