简体   繁体   English

检查文件是否存在以及是否不从嵌入式资源创建文件

[英]Checking if a file exists and if it doesn't creating it from embedded resource

I don't know why I can't get this to work, I have found lots of answers on google but none of them seem to work for me. 我不知道为什么我无法使用它,我在Google上找到了很多答案,但似乎没有一个适合我。

My program requires a file on disk called "wordlist.txt" at runtime, in the same directory as the executable. 我的程序在运行时需要磁盘上名为“ wordlist.txt”的文件,该文件与可执行文件位于同一目录中。 So I have in my constructor code to check whether it exists, and if not to create it by copping it from the embedded resource 所以我在构造函数代码中检查它是否存在,如果不存在,可以通过从嵌入式资源中获取它来创建它

public Form1()
{
    InitializeComponent(); 

    if (!File.Exists("wordlist.txt")) 
    {
        byte[] ba = File.ReadAllBytes(Properties.Resources.wordlist);
        File.WriteAllBytes("wordlist.txt", ba); 
    }
}

It tells me I have illegal characters in my path though. 它告诉我我的路径中有非法字符。

I think your are storing the default word list as a resource. 我认为您正在将默认单词列表存储为资源。 If you are, you probably need something like, 如果是,您可能需要类似的东西,

if (!File.Exists("wordlist.txt"))
{
    File.WriteAllText("wordlist.txt", Properties.Resources.wordlist);
} 

您是否尝试过使用完整路径,例如@“ c:\\ worldlist.txt”,而不仅仅是文件名?

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

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