简体   繁体   中英

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.

My program requires a file on disk called "wordlist.txt" at runtime, in the same directory as the executable. 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”,而不仅仅是文件名?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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