简体   繁体   English

在Windows 7下创建新文件和目录时,我的代码是否会导致崩溃?

[英]Could my code cause a crash when creating a new file and directory under Windows 7?

I have had some problems earlier which caused my program to crash in all Windows OS because I did not create a new file/directory for my file. 我之前遇到过一些问题导致我的程序在所有Windows操作系统中崩溃,因为我没有为我的文件创建新的文件/目录。 Then I have assured that I have created file/folder before initializing ect. 然后我确信在初始化ect之前我已经创建了文件/文件夹。 Now my program works in Windows XP but it doesn't work in Windows 7. By saying that it works I mean that it creates a file/folder needed for my program. 现在我的程序在Windows XP中运行,但它在Windows 7中不起作用。通过说它可行,我的意思是它创建了我的程序所需的文件/文件夹。 By not working I mean that the file/folder isn't created in windows 7. 不工作我的意思是文件/文件夹不是在Windows 7中创建的。

Could this code be the cause of the crash under Windows 7? 这段代码可能是Windows 7下崩溃的原因吗? If so, how could I fix it? 如果是这样,我该如何解决?

    private static string dir = Environment.GetFolderPath
        (Environment.SpecialFolder.ProgramFiles) + @"\folder\";
    private static string file = dir + @"\Settings.txt";
    private string text;

    public void CheckFileStatus()
    {
        if (!Directory.Exists(dir))
        {
            DirectoryInfo directory = Directory.CreateDirectory(dir);
        }
        if (!File.Exists(file))
        {
            using (FileStream fileStream = File.Create(file))
            {
            }
        }
    }

The program files directory in Windows 7 can only be written to with elevated privileges. Windows 7中的程序文​​件目录只能使用提升的权限写入。 Are you running your code as an administrator? 您是否以管理员身份运行代码? It's also bad practice to write to the program files folder. 写入程序文件文件夹也是不好的做法。 You should be using the %appdata% folder. 您应该使用%appdata%文件夹。

Take a look here to see the various special folders. 看看这里看各种特殊文件夹。 You will likely want to use either System.Environment.SpecialFolder.ApplicationData or System.Environment.SpecialFolder.CommonApplicationData . 您可能希望使用System.Environment.SpecialFolder.ApplicationDataSystem.Environment.SpecialFolder.CommonApplicationData This will allow you to write data without needing elevated privileges. 这将允许您在不需要提升权限的情况下编写数据。

You cannot create folders in Program Files without being having elevated privileges (ie acting as an Administrator) on Windows Vista and Windows 7. There are generally better places to put settings files that should be writable by any user. 如果没有在Windows Vista和Windows 7上具有提升的权限(即充当管理员),则无法在Program Files中创建文件夹。通常有更好的位置来放置任何用户都应该可写的设置文件。

Environment.SpecialFolder.ApplicationData or Environment.SpecialFolder.LocalApplicationData is generally the place for user specific application data which is most likely what you want. Environment.SpecialFolder.ApplicationDataEnvironment.SpecialFolder.LocalApplicationData通常是用户特定应用程序数据的位置,这很可能是您想要的。

The difference being that in a domain, ApplicationData will be placed in your roaming profile and be shared between computers on the domain, while LocalApplicationData is for that very machine only. 区别在于,在域中, ApplicationData将放置在您的漫游配置文件中,并在域中的计算机之间共享,而LocalApplicationData仅用于该计算机。

For home users or if you don't specifically want the data to be shared between machines, probably LocalApplicationData is better. 对于家庭用户或者如果您不希望在计算机之间共享数据,可能LocalApplicationData可能更好。 That way you know it won't cause problems on a domain if you end up writing computer specific data in it. 这样你知道如果最终在其中编写计算机特定数据,它不会在域上引起问题。

There is also Environment.SpecialFolder.CommonApplicationData which allows for sharing the same data between all users on the computer, but while that may seem convenient, consider that any user on the machine can then change settings of a program that is later run by you which may cause security implications. 还有Environment.SpecialFolder.CommonApplicationData允许在计算机上的所有用户之间共享相同的数据,但这看起来很方便,请考虑机器上的任何用户都可以更改稍后由您运行的程序的设置。可能会造成安全隐患。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM