Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Help with creating folder on user's PC

Posted on 2015-02-11
6
Medium Priority
?
89 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 34

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 34

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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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 34

Accepted Solution

by:
it_saige earned 2000 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 to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
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 …
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

609 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