共享在xamarin路径上的违反

时间:2023-02-10 07:53:28

I'm very new to Android programming. I have a code which creates a file in a designated folder and then tried to write something to it. Like below:

我对Android编程非常熟悉。我有一个代码,它在指定的文件夹中创建一个文件,然后尝试向它写一些东西。像下图:

        path = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments);
        var filename = Path.Combine(path, "Test.xml");
        Directory.CreateDirectory (path);
        if (!File.Exists (path + "/" + "Test.xml")) {
            File.Create (path + "/" + "Test.xml");
        }
        using (var streamWriter = new StreamWriter(filename, true))
        {
            streamWriter.WriteLine("<?xml version='1.0' encoding='utf-8'?>");
            streamWriter.WriteLine ("<Apples>");
            streamWriter.WriteLine ("</Apples>");
        }

In line using (var streamWriter = new StreamWriter(filename, true)), I'm getting the Sharing Violation on path error.

使用(var streamWriter = new streamWriter (filename, true)),我得到了路径错误的共享违例。

Could someone please point me as to exactly where I'm going wrong and provide me a solution.

谁能告诉我我到底哪里出错了,并给我一个解决方案吗?

Thanks, Anirban

谢谢,Anirban

2 个解决方案

#1


1  

Why do you create the file then reopen it to write to it. StreamWriter has an method that will do just that. It will create a new file if it doesn't exist.

为什么要创建这个文件,然后重新打开并写入它。StreamWriter有一个方法可以做到这一点。如果不存在,它将创建一个新文件。

Initializes a new instance of the StreamWriter class for the specified file on the specified path, using the default encoding and buffer size. If the file exists, it can be either overwritten or appended to. If the file does not exist, this constructor creates a new file.

使用默认编码和缓冲区大小初始化指定路径上的文件的StreamWriter类的新实例。如果文件存在,可以覆盖它,也可以附加到它。如果该文件不存在,此构造函数将创建一个新文件。

StreamWriter could not access the file because File.Create returned a FileStream you did not consume.

StreamWriter不能访问文件,因为文件。创建返回的未使用的文件流。

As mentioned above, the File.Create is not necessary. You could also use:

如上所述,文件。创建并不是必要的。你也可以使用:

using (var writer = new StreamWriter(File.Create(statusTxtPath)))
{
   // do work here.
}

which will consume the file stream and close it. Whenever working with streams and most classes that interact with streams, be sure to use the using() block to ensure handles are released properly.

它将使用文件流并关闭它。每当处理流和大多数与流交互的类时,请确保使用using()块确保正确释放句柄。

#2


1  

Ok...I have managed to resolve the issue...by using

好吧……我已经设法解决了这个问题。通过使用

using (var streamWriter = new StreamWriter (File.Create (path + "/" + "DoctorsList.xml")))

#1


1  

Why do you create the file then reopen it to write to it. StreamWriter has an method that will do just that. It will create a new file if it doesn't exist.

为什么要创建这个文件,然后重新打开并写入它。StreamWriter有一个方法可以做到这一点。如果不存在,它将创建一个新文件。

Initializes a new instance of the StreamWriter class for the specified file on the specified path, using the default encoding and buffer size. If the file exists, it can be either overwritten or appended to. If the file does not exist, this constructor creates a new file.

使用默认编码和缓冲区大小初始化指定路径上的文件的StreamWriter类的新实例。如果文件存在,可以覆盖它,也可以附加到它。如果该文件不存在,此构造函数将创建一个新文件。

StreamWriter could not access the file because File.Create returned a FileStream you did not consume.

StreamWriter不能访问文件,因为文件。创建返回的未使用的文件流。

As mentioned above, the File.Create is not necessary. You could also use:

如上所述,文件。创建并不是必要的。你也可以使用:

using (var writer = new StreamWriter(File.Create(statusTxtPath)))
{
   // do work here.
}

which will consume the file stream and close it. Whenever working with streams and most classes that interact with streams, be sure to use the using() block to ensure handles are released properly.

它将使用文件流并关闭它。每当处理流和大多数与流交互的类时,请确保使用using()块确保正确释放句柄。

#2


1  

Ok...I have managed to resolve the issue...by using

好吧……我已经设法解决了这个问题。通过使用

using (var streamWriter = new StreamWriter (File.Create (path + "/" + "DoctorsList.xml")))