Link to home
Start Free TrialLog in
Avatar of Stef Merlijn
Stef MerlijnFlag for Netherlands

asked on

Creating a shared folder

Hi,

Currently I use the code below to compose the UNC-path of a folder.
Basically this is done once at initial setup of my application. The UNC-path is stored in the database and all other paths in the database point to this base-folder. Nothing can be saved/created outside this base-folder, so I only store the relative path to it.

So when the basefolder is set to: \\COMPUTERNAME\C$\FOLDERNAME
and a file is stored in folder:           \\COMPUTERNAME\C$\FOLDERNAME\MyFolder\MyFile.txt
I only store the latter part of the filename: \MyFolder\MyFile.txt

Now the problem. If the administrator who is setting up my application is selecting/creating a base-folder (from within my application). Then this folder might not be shared. Normally this would not be a problem as long as the path isn't transferred to the UNC-path. From that moment on the folder isn't accessible anymore.
Created base-folder: C:\Foldername\Test\
UNC-path:                  \\COMPUTERNAME\C$\Foldername\Test\
Here folder "C:\Foldername\Test\" would be accessible to the admin, but folder "\\COMPUTERNAME\C$\Foldername\Test\" isn't.
Apparently in order to access an UNC-path it must be shared first.

My customers are for some part, stand-alone users on one computer. They don't bother sharing folders (maybe even never heard of it). Others however need to work with multiple users in my application (they might even have a systemadministrator).
Another thing to keep in mind:
The computer of the customer needs the be set in a correct way, so sharing of folders is possible. I know f.e. that a missing registry-value called "IRPStackSize" can influence sharing folders.
HKLM\SYSTEM\CurrentControlSet\Services\lanmanserver\parameters\IRPStackSize

So much for the background.

One possible approach could be to share the base-folder from within my application. The moment the base-folder is selected, it will be shared among all users.

What I would like from you is to come up with some workable solutions. After we agree upon the approach, then see what code is needed to make it happen.
function BuildUNCPath(vFolder : String) : String;  
begin  
  if Copy(vFolder,1,1) <> '\' then  
    Result := '\\' + GetComputerName + '\' + StringReplace(vFolder,':','$', [rfReplaceAll])  
  else  
    Result := vFolder;  
end;

Open in new window

Avatar of systan
systan
Flag of Philippines image

You don't have to share  "C:\Foldername\Test\"  because it is already in the UNC path of c$, when c$ is shared, then the rest of the folders inside drive c is shared too.
Avatar of Stef Merlijn

ASKER

Yes, but normally an administrator will not share a whole drive, but only certain files and folders.
Small companies (1 person) with one stand-alone computer never have to share anything, because they have only one administrator Windows-user.
Avatar of aflarin
aflarin

I think it may be the group of questions at initial setup:

1. Would you like to share data?

  No: nothing do
  Yes: user selects the base folder. then next question

  1.2 This path should be shared.
       (X) (recommended) Would you like to create shared folder automatically?
       (  ) If the share already exists, please select it [Select button]

When your program runs, it should check the basefolder and if it's empty or it doesn't exist (it may be deleted), the initial setup must be repeated

aflarin:
Exactly.
Sharing is required otherwise the application can't use the basefolder. Checking the existence is already in place, that's why I know that the folder required sharing :-)
But now how to do this.
  1. How to share the selected base-folder from within my application?
  2. How to select  an existing share (didn't know that was possible)?
ASKER CERTIFIED SOLUTION
Avatar of aflarin
aflarin

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
NetShareAdd
To whom will this code give access?
To ALL users or only to the logged in user? Or else?
NewShareEnum
In the example of NewShareEnum it states that it will also enumerate hidden shares (Like: C$).
Isn't such a share always available? I believe in the current situation I already use a hidden share (which doesn't work for me, as it will not allow access to the folder using this sharename). Or am I wrong?
Secondly:
How can I use the code that was presented? I never worked with a console application before. Can it easily be integrated into my application?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Your code works, but at the end of the line in the memo there is a column called "Descriptor:" filled with Yes or NO

  if IsValidSecurityDescriptor(p^[i].shi502_security_dsc) then  
    line:= line + 'Yes'  
  else  
    line:= line + 'No';
On my system only one share has YES.
The share that I added with NetShareAdd contains NO.
Also in Windows Explorer I can't see that this folder is shared (based on the looks of the foldericon). Is this a problem or will it work anyway?

What if the base-folder is a some subfolder.
f.e.: C:\MyFolder\Temp\MyBaseFolder
What happens if I share that folder? Will the user than also have access to the folders above?
You can forget the remark about:
"Also in Windows Explorer I can't see that this folder is shared (based on the looks of the foldericon). Is this a problem or will it work anyway?"
A refresh in Explorer did the trick.
Than another:
If I let the user select a share.
Do I in fact let him select a shared folder and will that need to become the base-folder?
Am I correct to assume selecting the base-folder first and share it, works exactly the other way around from this approach?
if the Descriptor field filled with NO - it means the share hasn't the associated descriptor. Unfortunatelly, I dodn't find the MSDN entry about that cause, but I think it means that such share has the default rights, i.e Everyone group + Full Control.

>> Will the user than also have access to the folders above?

NO

>> Do I in fact let him select a shared folder and will that need to become the base-folder?

Why not? It is the first way. I don't know if it suits to you

The second way if you already have the base folder and you don't want to change it. Then the Select button (from my first post) may be needed if user already have the above shared folder (or may be he would like to create the shared folder with some other rights).
When he press the Select button, you get all shares (NetShareEnum), leave only such shares which have LocalPath that includes your base folder and let him choose them.
If there isn't any shared folder that includes your base folder, you just show the warning message like:
"There is no any shares what includes BASE_FOLDER. Please create the shared folder or use the recommended way"
So far so good. Now using it all.
Computername = MYCOMP123
User selects a base-folder:
C:\Documents and Settings\All Users\Documenten\MyAppName
Suppose I always create my own share called 'MyApp'.
How would I access this shared folder from another computer in the network (using the computername)?
Something like: \\MYCOMP123\MyApp\  ??and then what??
Suppose some subfolders are created inside the base-folder.
C:\Documents and Settings\All Users\Documenten\MyAppName\Sub1
C:\Documents and Settings\All Users\Documenten\MyAppName\Sub2
Does the user have access to these subfolders?
>> How would I access this shared folder from another computer in the network (using the computername)?
>> Something like: \\MYCOMP123\MyApp\  ??and then what??

yes, you just use UNC path instead of local path. For example:

  Memo1.Lines.LoadFromFile( '\\MAIN\C$\test.txt' );

>> Does the user have access to these subfolders?

Yes:

Memo1.Lines.LoadFromFile( '\\MAIN\C$\temp\test.txt' );


BUT there is some problem with your example: C:\Documents and Settings\All Users\Documenten\
This folder has limited file-system rights by default (if your file-system is NTFS, of course). Only Power Users and Administrator can write it by default.

So if you need to access this folder from User account and for reading - there is no problem. otherwise you have a define other rights to
C:\Documents and Settings\All Users\Documenten\MyAppName

>> yes, you just use UNC path instead of local path.
So I still use the administrative sharename "C$" even though I created my own sharename "MyApp"?
>> C:\Documents and Settings\All Users\Documenten\
>> This folder has limited file-system rights by default  
I you would have to pick a folder on a NTFS, that needs to be shared by all user that use my application, which would it be?
And in addition:
If the base-folder is changed after a while, the old share needs to be replaced with the new one.
Is there a way to delete all Shares with a certain name (f.e. "MyApp")? Maybe from registry?
I've seen the entry in:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\lanmanserver\Shares
But I don't know if that is the only entry in registry to keep the shares?
How to remove a share?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
NetShareDel: works perfectly.
The only thing that is troubleing me is giving access rights to the base-folder that will be selected by the user. (bye the way: only admin-users can change the base-folder in my application).
The code you point to is not at all clear to me. Also it mentions that advapi32.dll must be added.
Would it be much trouble for you to make me some working sample? And tell me which files need to be added to the uses clause, and maybe even distributed with my app.

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you very much for your help. I realy appreciate it.