none
C#文件保存问题 RRS feed

  • 问题

  • 我自制了一个文档编辑器,文档的后缀当然是我自定,这事难就难在新建时将不存在的文件存在哪里?

    如果在新建时就给存到默认的地方,保存时再移动,应该行。

    但是以后再保存,就出现“文件正在读写,拒绝访问”的异常,怎么办?

    2018年3月28日 7:01

答案

  • Hi,

    欢迎在MSDN论坛发帖。

    你在创建文件的时候,没有指定创建的目录吗?你可以参考下面如何指定 顶级 子集的目录。

    public class CreateFileOrFolder
    {
        static void Main()
        {
            // Specify a name for your top-level folder.
            string folderName = @"c:\Top-Level Folder";
    
            // To create a string that specifies the path to a subfolder under your 
            // top-level folder, add a name for the subfolder to folderName.
            string pathString = System.IO.Path.Combine(folderName, "SubFolder");
    
            // You can write out the path name directly instead of using the Combine
            // method. Combine just makes the process easier.
            string pathString2 = @"c:\Top-Level Folder\SubFolder2";
    
            // You can extend the depth of your path if you want to.
            //pathString = System.IO.Path.Combine(pathString, "SubSubFolder");
    
            // Create the subfolder. You can verify in File Explorer that you have this
            // structure in the C: drive.
            //    Local Disk (C:)
            //        Top-Level Folder
            //            SubFolder
            System.IO.Directory.CreateDirectory(pathString);
    
            // Create a file name for the file you want to create. 
            string fileName = System.IO.Path.GetRandomFileName();
    
            // This example uses a random string for the name, but you also can specify
            // a particular name.
            //string fileName = "MyNewFile.txt";
    
            // Use Combine again to add the file name to the path.
            pathString = System.IO.Path.Combine(pathString, fileName);
    
            // Verify the path that you have constructed.
            Console.WriteLine("Path to my file: {0}\n", pathString);
    
            // Check that the file doesn't already exist. If it doesn't exist, create
            // the file and write integers 0 - 99 to it.
            // DANGER: System.IO.File.Create will overwrite the file if it already exists.
            // This could happen even with random file names, although it is unlikely.
            if (!System.IO.File.Exists(pathString))
            {
                using (System.IO.FileStream fs = System.IO.File.Create(pathString))
                {
                    for (byte i = 0; i < 100; i++)
                    {
                        fs.WriteByte(i);
                    }
                }
            }
            else
            {
                Console.WriteLine("File \"{0}\" already exists.", fileName);
                return;
            }
    
            // Read and display the data from your file.
            try
            {
                byte[] readBuffer = System.IO.File.ReadAllBytes(pathString);
                foreach (byte b in readBuffer)
                {
                    Console.Write(b + " ");
                }
                Console.WriteLine();
            }
            catch (System.IO.IOException e)
            {
                Console.WriteLine(e.Message);
            }
    
            // Keep the console window open in debug mode.
            System.Console.WriteLine("Press any key to exit.");
            System.Console.ReadKey();
        }
        // Sample output:
    
     }

    你可以告诉,你是在哪个代码处,出现问题的吗? 同一时刻,在访问共有资源时,可以加锁保护一下。

    Best Regards,

    Hart


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • 已标记为答案 czhch 2018年4月8日 6:23
    2018年3月29日 2:16
    版主

全部回复

  • 难在新建时将不存在的文件存在哪里?

    可否贴出部分代码以分析文件的保存位置,一般是EXE当前位置,系统Temp临时文件夹,或自定义的位置。


    专注于.NET ERP/CRM开发框架,C/S架构,SQL Server + ORM(LLBL Gen Pro) + Infragistics WinForms

    2018年3月29日 0:41
  • Hi,

    欢迎在MSDN论坛发帖。

    你在创建文件的时候,没有指定创建的目录吗?你可以参考下面如何指定 顶级 子集的目录。

    public class CreateFileOrFolder
    {
        static void Main()
        {
            // Specify a name for your top-level folder.
            string folderName = @"c:\Top-Level Folder";
    
            // To create a string that specifies the path to a subfolder under your 
            // top-level folder, add a name for the subfolder to folderName.
            string pathString = System.IO.Path.Combine(folderName, "SubFolder");
    
            // You can write out the path name directly instead of using the Combine
            // method. Combine just makes the process easier.
            string pathString2 = @"c:\Top-Level Folder\SubFolder2";
    
            // You can extend the depth of your path if you want to.
            //pathString = System.IO.Path.Combine(pathString, "SubSubFolder");
    
            // Create the subfolder. You can verify in File Explorer that you have this
            // structure in the C: drive.
            //    Local Disk (C:)
            //        Top-Level Folder
            //            SubFolder
            System.IO.Directory.CreateDirectory(pathString);
    
            // Create a file name for the file you want to create. 
            string fileName = System.IO.Path.GetRandomFileName();
    
            // This example uses a random string for the name, but you also can specify
            // a particular name.
            //string fileName = "MyNewFile.txt";
    
            // Use Combine again to add the file name to the path.
            pathString = System.IO.Path.Combine(pathString, fileName);
    
            // Verify the path that you have constructed.
            Console.WriteLine("Path to my file: {0}\n", pathString);
    
            // Check that the file doesn't already exist. If it doesn't exist, create
            // the file and write integers 0 - 99 to it.
            // DANGER: System.IO.File.Create will overwrite the file if it already exists.
            // This could happen even with random file names, although it is unlikely.
            if (!System.IO.File.Exists(pathString))
            {
                using (System.IO.FileStream fs = System.IO.File.Create(pathString))
                {
                    for (byte i = 0; i < 100; i++)
                    {
                        fs.WriteByte(i);
                    }
                }
            }
            else
            {
                Console.WriteLine("File \"{0}\" already exists.", fileName);
                return;
            }
    
            // Read and display the data from your file.
            try
            {
                byte[] readBuffer = System.IO.File.ReadAllBytes(pathString);
                foreach (byte b in readBuffer)
                {
                    Console.Write(b + " ");
                }
                Console.WriteLine();
            }
            catch (System.IO.IOException e)
            {
                Console.WriteLine(e.Message);
            }
    
            // Keep the console window open in debug mode.
            System.Console.WriteLine("Press any key to exit.");
            System.Console.ReadKey();
        }
        // Sample output:
    
     }

    你可以告诉,你是在哪个代码处,出现问题的吗? 同一时刻,在访问共有资源时,可以加锁保护一下。

    Best Regards,

    Hart


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • 已标记为答案 czhch 2018年4月8日 6:23
    2018年3月29日 2:16
    版主
  • 我在做帮助文件,虽然Windows已经有帮助文件,但是原有的帮助文件的编码没有统一码,所以字显示不全,我就想自己做。
    2018年3月29日 10:18