Create Document in the Folder the EXE is in (VB) - vb.net

I'm starting to play around with FileStream to make a text document. When you do this, you have to clarify a path. Is there a way to create the text document in the folder the EXE file is in?
(I'm asking this because this program is meant to be downloaded, so I think I can't clarify a path specific to my computer)
Thank you!

You're right, you can't bake a path into your program that is specific to your computer because then it won't work on the user's computer
Jimi makes the wise point that often programs are installed to C:\Program Files or similar and it's not automatically possible to write to subfolders in there - you'll have to get into asking the user for permission (Elevation) .. headache
Better to decide what you want the path for:
If you need a temporary path to e.g. download something to then throw it away you can call Path.GetTempFilename() or Path.GetTempPath() - the former creates a 0 byte file with a random name in the user's temp folder, and returns the path. The latter gives you the path to the temp folder so you can create your own file
If the file is to have some permanence, such as the user saving his work, you should ask the user for it. SaveFileDialog and FolderBrowserDialog are two things you can drop on your windows form and then call ShowDialog() on to show the uer a UI where they pick a path. After they OK or Cancel, you check if they OK'd or Cancel and proceed using the dialog's Filename or SelectedPath respectively (if they OK'd)
When you're writing your files it's easier not to use FileStream unless you really need to seek in the file etc. Easier to just:
System.IO.File.WriteAllText(path here, contents here)
If you have to write the contents of a string variable to a file

The best way to create a text file, would be to use CreateText method. It will create a file besides the executable program file. You can go the following way.
Dim sw as StreamWriter = File.CreateText("myfile.txt")
Dim str as String = "Your text"
sw.Write(str)
sw.Flush()
sw.Close()

Related

change of location of the database dynamically in app running

Try to make my app read the ms-access database from shortcut of my database it failed to read, so try to change the location of database dynamically ( there is an option in the app to move the database to drop-box folder and create a shortcut to that database in app folder )
try to make an shortcut to the moved ms-database
the app to read the database or to change the location of database dynamically
First of all, you should create a folder with a clear name in your VB.Net application path, namely inside the project Debug folder, let's name that folder as "MyProjFiles", so it will be in this path: ProjectFolderName\bin\Debug\MyProjFiles
Put your whole projects files inside our lovely folder MyProjFiles, including all types of your attachments: database, images, sounds, files, etc.
Call your database or whatever of those attachments files in addition to our \MyProjFiles\ using this method: My.Computer.FileSystem.CurrentDirectory & "\MyProjFiles\YourFilesPathHere.EXT".
Now, the whole path will be such as this string: "C:\CurrentUserNam\RootFolder\ProjectFolderName\bin\Debug\MyProjFiles\YourFilesPathHere.EXT"
For great practical example of this, supposuply let's open our MSAccessDB.accdb which is already copied into our project folder \MyProjFiles\ by this code directly:
System.Diagnostics.Process.Start(My.Computer.FileSystem.CurrentDirectory() & "\MyProjFiles\MSAccessDB.accdb")
The result will be simply opening our database which called "MSAccessDB.accdb"
Or open some pdf files such as this line:
System.Diagnostics.Process.Start(My.Computer.FileSystem.CurrentDirector() & "\MyProjFiles\MyPdfFile.pdf")
and so on.
I hope this can help you all brothers.
Best ^_^ Regards.
You should read this link.
It explains how to read the information you need to give the access Datareader something to do.
.NET read binary contents of .lnk file
Maybe this is enough, so you don't need to copy anything.

VB.Net Run.exe with Form button

I'm pretty new to VB so I need a bit of help with some coding.
I made a program that should download a certain file to what ever directory the user wants, but now i want my program to run that certain file but i don't know how to do it since the user used his/her own directory.
Like i want the program to run that certain file no matter where its saved, how do i do that?
I do know this code:
System.Diagnostics.Process.Start("C:\")
but this doesnt really work since i have to put in a directory before i use the program.
You can get the path of the SFD by using filename.
Dim Path as String
Path = SaveFileDialog.FileName
Then run the file.
System.Diagnostics.Process.Start(Path)
Found another way of doing it,
I just used:
file.open(textbox1.text)
And made it take the text forom the the other textbox the user browsed the file to

Create Folder and save file through fileopenDialog

i am using FileOpenDialog control to save file in C: drive,
Now i want to create New Folder and save the file in that folder with FileOpenDialog.
and also from other forms i have to store different file in that same foldr.
Please any one suggest me.
i think this link may help you
http://social.msdn.microsoft.com/Forums/vstudio/en-US/9bc2c92a-b22f-484c-9a15-0aebf639422d/folder-browser-dialog-in-vbnet-2010
by the way you should use SavefFileDialog or FolderBrowserDialog
First of all, you should use the SaveFileDialog to save the file. Not the file open dialog. The SaveFileDialog control will automatically let the user also create a folder -- just open the dialog in run-time mode and right click inside and see.. New > New Folder. Once the user navigates into this newly created folder and specifies the file name, all you need to do is to save the file.
Alternately, if you want to use a fixed subfolder, don't prompt the user to create the folder for you. Instead after the dialog is dismissed, do this --
string savePath = saveFileDialog1.FileName;
string directory = System.IO.Path.GetDirectoryName(savePath);
savePath = string.Format(
"{0}\\MySubFolder\\{1}",
directory,
System.IO.Path.GetFileName(savePath)
);
Save the file to the path in "savePath".
(PS: Sorry my above snippet is in C#, but you should be able to easily convert it to VB.NET).

Make my applescript process specified files in the current directory?

I need to generate PDFs from word files regularly, and I'm getting tired of doing it by hand.
Manually, all I do is open a file, and click "Save as PDF". So, one would think applescript would be a nice easy way to do this. [If you have another approach than applescript, I'm open to it.]
I'm almost there, the following script works, except the full path is hard coded.
tell application "Microsoft Word"
open file "Macintosh HD:Users:me:repos:training:class:Activities:ActivityGuide.docx"
set doc to document "ActivityGuide.docx"
save as doc file name "Macintosh HD:Users:me:repos:training:class:Activities:ActivityGuide.pdf" file format format PDF
end tell
I need it to work for other people, on other machines, so the username and other parts of the path may change. If I could just do this from the current directory for the script, I'd be set.
I found this: Applescript to launch file from current folder?
tell application "Finder"
open file "somefile.txt" of folder of (file (path to me))
end tell
Which works for opening an app from the current directory in Word, via Finder, but I think I need to open it with an application of "Microsoft Word", if I'm going to use "Save As". But this method of opening doesn't work if I change the application for "Finder" to "Microsoft Word".
Any suggestions welcome.
[edit: clarity]
Try this
set x to path to me
tell application "Finder" to set tFile to (file "ActivityGuide.docx" of (container of x)) as alias
set newFile to (text 1 thru -5 of (tFile as string)) & "pdf"
tell application "Microsoft Word"
open tFile
tell document 1
save as file name newFile file format format PDF
close saving no
end tell
end tell
Another thing that might be helpful to you is under System Preferences:Keyboard:Shortcuts: App Shortcuts:All Applications
Make a shortcut that is named "Save as PDF..." assign the shortcut to command+P since most applications that is the shortcut for print, then just press it twice and it will prompt you to save it as a PDF (I got that tip from David Sparks) and depending on the application it will default to where the file was opened up from. You can also set that with the application Default Folder X.
Hopefully that is a bit more of a global solution for other problems but it looks like you already have the answer to this isolated problem.

get the installed path of a vb.net application

I'm nearly ready to distribute my vb.net application. I have several picturebox files which are loaded currently from c:/temp
I need to change this directory to one that will be OK to use when the user installs it to their PC.
My question is how can I do this? Is there a way to get the installation path, then use that within the code as a variable? eg: myInstalledPath & "/xxx.jpg"
Or... would it be better for me to use mypictures within the mydocuments structure? I'd rather keep all the image files created in a folder which is more hidden from the user (by hidden I mean not cluttering up their own image folders!)
I've tried searching for this, but I seem to get varying results with no real answers... (possibly searching for the wrong thing!)
You can get the ExecutablePath with:
Dim appPath As String = Path.GetDirectoryName(Application.ExecutablePath)
Then you'll know where the application is residing.
As for where to save your images a common location is the AppData folder.
You can get it like this:
Dim appDataPath As String = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
The AppData folder is by default a hidden folder so that satisfies your requirements.