Solved

Help with creating folder on user's PC

Posted on 2015-02-11
6
85 Views
Last Modified: 2015-02-18
Hi,

I am using clickonce technolody to deploy my project, would like to create a folder on user's PC to save a file, does the user need admin rights to create a folder on the C drive? If not, How do you create a folder (i.e. AP40) on the C drive using VB.NET?

Thanks,

Victor
0
Comment
Question by:vcharles
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 33

Expert Comment

by:it_saige
ID: 40604496
Creating a folder is easy (quick and dirty)
Imports System.IO

Module Module1
	Sub Main()
		CreateFolder("C:\Test")
		Console.ReadLine()
	End Sub

	Public Sub CreateFolder(ByVal folder As String)
		Dim dir As DirectoryInfo = New DirectoryInfo(folder)
		Try
			If Not dir.Exists Then
				dir.Create()
				Console.WriteLine("Directory {0}; created.", dir.FullName)
			Else
				Console.WriteLine("Directory {0}; already exists.", dir.FullName)
			End If
		Catch ex As Exception
			dir = New DirectoryInfo(folder)
			If Not dir.Exists Then
				Console.WriteLine("There was an error creating the directory - {0}.", dir.FullName)
			End If
			Console.WriteLine("{0}.", ex.Message)
		Finally
			If dir IsNot Nothing Then
				dir = Nothing
			End If
		End Try
	End Sub
End Module

Open in new window

Produces the following output -Capture.JPGBut, you asked a key question.
Does the user need admin rights?
It depends on the operating system (Vista and above, YES).  In order to provide admin rights on the creation of the folder, you could use an API call to ShellExecute.  You could also instruct the user to right-click on the installer and run as administrator.  Or, you could use a manifest file to instruct the application that it needs administrative rights.

But this raises another couple of questions.

Is the folder user or application specific?
Is there a dependency that requires the folder to be on the root of C:?

If the folder is specific to your application and is only needed for the installation, it is better to use a temporary file location that already exists and can be accessed by way of the environment variables.  If it is permanent and required, then if it is user specific, put it in the Users Application Data folder (again accessible through environment variables).  Otherwise, store it in the application folder.

-saige-
0
 
LVL 33

Expert Comment

by:it_saige
ID: 40604520
Something like this illustrates what I am referencing:
Imports System.IO

Module Module1
	Sub Main()
		CreateFolder("C:\Test")
		' Creating a user specific folder
		CreateFolder(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "EE_Q28615221\AP40"))
		' Creating an application specific folder
		CreateFolder(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "EE_Q28615221\AP40"))
		Console.ReadLine()
	End Sub

	Public Sub CreateFolder(ByVal folder As String)
		Dim dir As DirectoryInfo = New DirectoryInfo(folder)
		Try
			If Not dir.Exists Then
				dir.Create()
				Console.WriteLine("Directory {0}; created.", dir.FullName)
			Else
				Console.WriteLine("Directory {0}; already exists.", dir.FullName)
			End If
		Catch ex As Exception
			dir = New DirectoryInfo(folder)
			If Not dir.Exists Then
				Console.WriteLine("There was an error creating the directory - {0}.", dir.FullName)
			End If
			Console.WriteLine("{0}.", ex.Message)
		Finally
			If dir IsNot Nothing Then
				dir = Nothing
			End If
		End Try
	End Sub
End Module

Open in new window

Produces the following output -Capture.JPG-saige-
0
 

Author Comment

by:vcharles
ID: 40604534
Hi,

Thank you for both inputs, due to admin rights issues with Vista or higher, better approach will be to allow users to select the folder they want to save the file.

Thanks,

Victor
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:vcharles
ID: 40604912
Hi,

Do you know if all windows pc have a c:\Temp folder, might be easier  to install to the temp folder.
0
 
LVL 33

Accepted Solution

by:
it_saige earned 500 total points
ID: 40604949
All windows computers do have a TEMP folder, but they are not all on the root of C:.  Also, based on Software Group Policies or just general network/computer security, it is a common practice to limit certain *tasks* from being accomplished in the TEMP folder (think restricting the ability to run or start a program and/or script from the TEMP folder).

If this is not a concern and for completeness, to access the Temp folder (regardless of it's defined location), you would use the GetTempPath() method in the Path class (which uses the GetTempPath API function).
The GetTempPath function checks for the existence of environment variables in the following order and uses the first path found:

      1.The path specified by the TMP environment variable.
      2.The path specified by the TEMP environment variable.
      3.The path specified by the USERPROFILE environment variable.
      4.The Windows directory.

Note that the function does not verify that the path exists, nor does it test to see if the current process has any kind of access rights to the path. The GetTempPath function returns the properly formatted string that specifies the fully qualified path based on the environment variable search order as previously specified. The application should verify the existence of the path and adequate access rights to the path prior to any use for file I/O operations.
Here is the above program using GetTempPath.
Imports System.IO

Module Module1
	Sub Main()
		' Creating an application folder in the temp directory
		CreateFolder(Path.Combine(Path.GetTempPath(), "EE_Q28615221\AP40"))
		Console.ReadLine()
	End Sub

	Public Sub CreateFolder(ByVal folder As String)
		Dim dir As DirectoryInfo = New DirectoryInfo(folder)
		Try
			If Not dir.Exists Then
				dir.Create()
				Console.WriteLine("Directory {0}; created.", dir.FullName)
			Else
				Console.WriteLine("Directory {0}; already exists.", dir.FullName)
			End If
		Catch ex As Exception
			dir = New DirectoryInfo(folder)
			If Not dir.Exists Then
				Console.WriteLine("There was an error creating the directory - {0}.", dir.FullName)
			End If
			Console.WriteLine("{0}.", ex.Message)
		Finally
			If dir IsNot Nothing Then
				dir = Nothing
			End If
		End Try
	End Sub
End Module

Open in new window

Which produces the following output -Capture.JPG-saige-
0
 

Author Closing Comment

by:vcharles
ID: 40617009
Thank You.
0

Featured Post

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question