none
那个啥,请问各位大神,为何Win10系统通过C#程序,无法创建文件 RRS feed

  • 问题

  • 请问各位大神,为何Win10系统通过C#程序,无法创建文件,源码如下,各位大神可以亲自测试一下:

    string docPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
                string tempPath =Path.Combine(docPath, "Erp");
                string path = Path.Combine(tempPath, "userinfo.config");
                //string path = AppDomain.CurrentDomain.BaseDirectory + "userinfo.config";
                if (!File.Exists(path))
                {
                    Directory.CreateDirectory(tempPath);
                    File.Create(path,2048, FileOptions.WriteThrough);
                }

    以上代码可以直接使用,忘各位大神亲测一下!直接建个Windows窗体应用程序,加个按钮即可。

    2017年12月21日 11:46

答案

  • Hi,

    欢迎在MSDN论坛发帖。

    我这边简单的创建一个C# console 工程,没有使用administrator 权限。

    是可以在C盘下面创建文件夹,以及文件的。

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace create_subfolder
    {
        class Program
        {
            static void Main(string[] args)
            {
                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


    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.

    2017年12月22日 2:58
    版主

全部回复

  • 无法创建文件的异常原因是什么。我猜测是没有文件夹的写入权限。


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

    2017年12月22日 0:56
  • 但是Win7操作系统是没有问题的,在Win10中就有问题,那么如何处理呢?有什么好的解决办法呢?求MSDN上各位大神,悉心赐教,谢谢!!!
    2017年12月22日 1:49
  • Hi,

    欢迎在MSDN论坛发帖。

    我这边简单的创建一个C# console 工程,没有使用administrator 权限。

    是可以在C盘下面创建文件夹,以及文件的。

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace create_subfolder
    {
        class Program
        {
            static void Main(string[] args)
            {
                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


    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.

    2017年12月22日 2:58
    版主