简体   繁体   中英

How can i lock a file, by making that it can only open using my application?

I want to lock a file ( lets use a image for example ). So I have this image "myImage.jpg" and I want it to only be accessible by using my application, such as my own image visualizer. The problem is that i dont know how to do it. Someone have suggested me to alter its extension ( myImage.jpg to myImage.whatever ). But someone could just alter it back to jpg, or click the "open with" button and select any image visualizer and it would work. So is that a way of doing it only accessible trough my app? And is there a way to make such an app that could lock the image that way? And is it possible to do it manually? I only know C# ( and im not an exepert on it yet ) in Visual Studio so...

You can control whether others can read/write the file while you're using it .

But once your application is closed, the file just sits there, and can be passed through the same reading logic you would execute, but by a different program. You could flip all the bytes of the file and hope "they" don't figure it out. You could encrypt it and decrypt it in run-time, if you really want to go far.

You can't prevent someone from deleting or renaming or moving your file, however, when you aren't using the file yourself.

No, there's no way to lock a file in the manner you're asking about. All files consist of a structure of bytes, and there's no way to "lock out" other applications from reading from or writing to a file.

If you want, you could hold open a handle to the file - FileStream's constructor takes overloads that specify file sharing modes, and one of those modes is 'no sharing'.

FileStream stream = new FileStream( 
    sourcePath, 
    FileMode.Open, 
    FileAccess.ReadWrite, 
    FileShare.None
);

As long as you hold that stream open, nothing else will be able to access the file, read or write.

您可以将文件保存为程序只能理解的格式,例如,通过在一定数量的字节前添加前缀。

I would like to say an approach which sparked on my mind.

zip the image/ file using encrypted password. unzip with password and and use that image/file.

Thanks,

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