LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Saving files in program files

Hey everyone, I am using CVI 2013 for an academic research.


In the program I made, I allowed the user to choose any directory for the output files using "DirSelectPopup".

 

The problem is when the user choose a protected directory, such as "C:\program files\" (which is, I think, the default folder if the user chose to install the program in c:\program files\) 

 

The output files are in "schrodinger's cat mode" as you can see in the first figure. I can see the files when I try to save files there again, but can not open them. However, as you can see in the second figure, when I try to open the folder manually, the folder is empty and weighted 0 bytes.

I believe that the reason is that program files are considered as highly protected folder. The question is whether I could warn the user from saving in any protected folder. 
I tried the following:
sprintf (FileName, "%s\\%s",DirName,SileName);   

FILE *matchFile = fopen(FileName, "w"); 
if (matchFile == NULL) return 1;
if(FileExists(FileName, 0) != 1) { .... }
But it doesn't work. is there any other exist command in CVI that I could use?

Thanks!

 

Download All
0 Kudos
Message 1 of 11
(3,814 Views)

Hi RaVen88,

 

For one thing, you can try showing hidden folders/files and see if you can view them after changing that. Here are some instructions on how to do that:

 

https://support.microsoft.com/en-us/help/14201/windows-show-hidden-files

 

Cheers,

Ryan C.

0 Kudos
Message 2 of 11
(3,753 Views)

Hey Ryan,

 

 

Thank you for your replay.

 

The option to show hidden files and folders was already on - I can't view the files through Windows, but can view them (just view) when I try to save there new files using the "DirSelectPopup" command.

0 Kudos
Message 3 of 11
(3,746 Views)

Hi RaVen88,

 

From what I've seen, the best way to do this is to use the system function in CVI to run the icacls command. The system function is documented here:

 

http://zone.ni.com/reference/en-XX/help/370051V-01/cvi/libref/cvisystem/

 

And you can see a discussion about the icacls command here:

 

https://superuser.com/questions/364083/windows-list-files-and-their-permissions-access-in-command-li...

 

Cheers,

Ryan

0 Kudos
Message 4 of 11
(3,725 Views)

Hey Ryan,

Unfortunately it looks very complex for me 😞


I have used the commands LaunchExecutable and system() before, but I have no idea how to save the information I get from the command into a variable.

Would you be kind to give me an example?

 

Thank you!

0 Kudos
Message 5 of 11
(3,685 Views)

Hi RaVen88,

 

Sure thing! Here is an example where they force the program to output to a file first, and then they read the file in the CVI program:

 

http://www.ni.com/example/27208/en/

 

I know this method is a bit slow, but it sounds like this will be a one-time operation since you're just checking file permissions. You can also just delete this temporary file right after you read from it to save hard drive space.

 

Cheers,

Ryan C.

0 Kudos
Message 6 of 11
(3,667 Views)

Hi RaVen88,

 

I was able to find a better way for you to do this. We actually have a function called GetFileWritability:

 

http://zone.ni.com/reference/en-XX/help/370051AC-01/toolslib/functionreference/cvigetfilewritability...

 

You can just pass it a file that you plan to write, whether or not it exists, and it will tell you if you can write the file in that directory. Here's a quick example that I used to verify that this works:

 

int writable = 0;
GetFileWritability("C:\\Program Files (x86)\\example.txt", &writable);
printf("Writable? %i", writable);

 

Cheers,

Ryan C.

Message 7 of 11
(3,661 Views)

Hey Ryan,

Thank you so much for your help, but unfortunately it did not work.. 

I tried to implement the code you wrote into my program and it always gives the value 1, even when I tried directly:

 

GetFileWritability("C:\\Program Files (x86)\\example.txt", &writable);

I think that the source of the problem is that I have an administrator access as a user, but the software does not.
when I try to open the software with the "open as administrator" option the files are indeed saved and work properly.
May I ask whether you get the value 0 for "GetFileWritability" also as an administrator user (but without using the "open as administrator" option)? maybe I do something wrong.

Thank you!

 

 

0 Kudos
Message 8 of 11
(3,643 Views)

Hey RaVen88,

 

I have to run the software as an administrator to have GetFileWritability return a 1. I am also an administrative user of my computer, but I think you're right that the software does not get administrator privilege unless you run as an admin. It won't be able to write files if it's run normally.

 

Does this match your behavior or are you seeing something else?

 

Cheers,

Ryan C.

0 Kudos
Message 9 of 11
(3,632 Views)

Hey Ryan,

 

Yes, this match the behavior.

 

Thank you for the replay!

So I guess the new question is how to know whether a folder is system protected (like program files) when you are an administrator user but did not open the software under administrator privileges (without the open as admin option).

It is really weird phenomenon, because in that state when I use for instance fopen(test.txt,"w") in the program files and then use FileExists() on it, I get the 'true' value but the files are not really exist. 

The reason it is important for me is that most of the programs uses the "Program Files" folder as a default for the setup, but it is a problem if another user won't be able to save files there without giving him any explanation. 

 

0 Kudos
Message 10 of 11
(3,628 Views)