Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/297.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#_Json_Substring_Data Manipulation_Streamwriter - Fatal编程技术网

C# 如何用C删除文件的最后一个字符

C# 如何用C删除文件的最后一个字符,c#,json,substring,data-manipulation,streamwriter,C#,Json,Substring,Data Manipulation,Streamwriter,您好,我是C初学者,我想删除文件的最后一个字符,手动将JSON对象注入到该文件中。我知道这不是最好的方法,因此我可以通过多种方法获得正确的格式,如打开文件,处理删除最后一个字符的字符串时,当我尝试替换同一文件中的文本时,会出现类似IOException的错误:进程无法访问文件“文件路径”,因为它正被其他进程或系统使用。UnauthorizedAccessException:“访问路径”C:\Users\ASUS\Desktop\Root”被拒绝 我将向您展示代码: StoreLogs Log =

您好,我是C初学者,我想删除文件的最后一个字符,手动将JSON对象注入到该文件中。我知道这不是最好的方法,因此我可以通过多种方法获得正确的格式,如打开文件,处理删除最后一个字符的字符串时,当我尝试替换同一文件中的文本时,会出现类似IOException的错误:进程无法访问文件“文件路径”,因为它正被其他进程或系统使用。UnauthorizedAccessException:“访问路径”C:\Users\ASUS\Desktop\Root”被拒绝

我将向您展示代码:

StoreLogs Log = new StoreLogs()
            {
                Id = ID,
                DateTime = dateT,
                TaskName = task,
                SrcAddress = srcPath,
                DstAddress = path,
                FileSize = DirSize(new DirectoryInfo(srcPath)),
                DelayTransfer = ts.Milliseconds,
            };

            // Record JSON data in the variable
            string strResultJson = JsonConvert.SerializeObject(Log);

            // Show the JSON Data
            // Console.WriteLine(strResultJson);

            // Write JSON Data in another file

            string MyJSON = null;
            string strPath = @"C:\Users\ASUS\Desktop\Backup\logs\log.json";



            if (File.Exists(strPath))
            {
                //FileInfo table = new FileInfo(strPath);
                //string strTable = table.OpenText().ReadToEnd();
                //string erase = strTable.Remove(strTable.LastIndexOf(']'));
                //Console.WriteLine(erase);

                //StreamReader r1 = new StreamReader(strPath);
                //string strTable = r1.OpenText().ReadToEnd();
                //string erase = strTable.Remove(strTable.LastIndexOf(']'));
                //r1.Close();

                using (StreamReader sr = File.OpenText(strPath))
                {
                    string table = sr.ReadToEnd();
                    string erase = table.Remove(table.LastIndexOf(']'));
                    sr.Close();
                    File.WriteAllText(strPath, erase);
                }



                //MyJSON = "," + strResultJson;
                //File.AppendAllText(strPath, MyJSON + "]");
                //Console.WriteLine("The file exists.");
            }
            else if (!File.Exists(strPath))
            {
                MyJSON = "[" + strResultJson + "]";
                File.WriteAllText(strPath, MyJSON);
                Console.WriteLine("The file doesn't exists.");
            }
            else
            {
                Console.WriteLine("Error");
            }


            // End
            Console.WriteLine("JSON Object generated !");


            Console.ReadLine();
这就是我想要的结果: [{Id:8484,日期时间:2019年11月26日02:33:35,任务名:dezuhduzhd,SrcAddress:C:\\Users\\ASUS\\Desktop\\Root,DstAddress:C:\\Users\\ASUS\\Desktop\\Backup,文件大小:7997832.0,延迟传输:0.0},{Id:8484,日期时间:2019年11月26日02:33:35,任务名:dezuhduzhd,SrcAddress:C:\\Users\\ASUS\\Desktop\\Root,DstAddress:C:\\Users\\ASUS\\Desktop\\Backup,文件大小:7997832.0,延迟传输:0.0},{Id:8484,日期时间:2019年11月26日02:33:35,任务名称:dezuhduzhd,SrcAddress:C:\\Users\\ASUS\\Desktop\\Root,DstAddress:C:\\Users\\ASUS\\Desktop\\Backup,文件大小:7997832.0,延迟传输:0.0}]

编辑: 谢谢大家的建议

解决方案:


它说的是运行应用程序的用户对路径C:\Users\ASUS\Desktop\Root的访问被拒绝。例如:如果您是在user1 windows登录上从Visual studio运行的,则user1应该对该根文件夹具有适当的权限。如果代码本身运行exe,则检查调用该exe的用户的访问权限e、

在您发布的代码示例中,您正在打开一个流以读取该文件。退出该块后,使用块将处理该流。您正在尝试写入该文件,而读取流仍在访问该文件。读取流仍然存在。您基本上已打开该文件,从中读取,并尝试回写该文件但仍保持其打开状态。这是一个问题,原因是您没有使用流进行写入。因此,您的第二个写入进程无法访问该文件。我看到您在写入之前关闭流,但我敢打赌它仍保持引用打开状态

我会尝试这种方法:

根据您发布的错误,似乎:

可能您正在留下一些指向要编辑的文件的流,请使用“using”语句来避免这种情况

如果您试图访问一个文件,但您没有所需的权限,或者您不是系统管理员,或者文件是只读的,请尝试更改文件位置或将其设置为可写


希望这对您有所帮助!

您的意思是要打开srcPath而不是strPath吗?因此消息是准确的:要么文件在写入之前保持打开状态,要么您没有写入权限。我会排除一种可能性,即首先将.json文件放在您的执行文件夹中,以消除权限问题。第二,我将写入一个新文件,然后在成功后,删除原始文件,并将新文件重命名为最终名称。这样,如果写入文件时出错,您不会意外截断它。我遇到了另一个错误系统。ArgumentOutOfRangeException:'StartIndex不能小于零。我不这样认为,因为只有一个u在我的计算机中设置了ser,我检查了正确性您的代码在我的计算机上运行,没有任何问题。如果您在文件“]”中没有该字符,则它会抛出错误System.ArgumentOutOfRangeException。感谢您的回答,我找到了解决方案,并提供了您的建议。
FileStream fs = new FileStream(strPath, FileMode.Open, FileAccess.ReadWrite);
            fs.SetLength(fs.Length - 1);
            fs.Close();