Solved

Help with creating folder on user's PC

Posted on 2015-02-11
6
70 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
  • 3
  • 3
6 Comments
 
LVL 32

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 32

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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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 32

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

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 …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

747 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now