Click here to Skip to main content
14,391,319 members
Rate this:
Please Sign up or sign in to vote.
See more:
What i'm trying its open text file like this:
Dim FILE_NAME As String = "C:\FileName.txt"

If System.IO.File.Exists(FILE_NAME) = True Then
    Process.Start(FILE_NAME)
Else
    MsgBox("File Does Not Exist")
End If

But the problem is that, when i give my .exe with those files to another user, he might have another name of HardDrive, D: except C: for example.. therefore this:
"C:\FileName.txt"
will not find a file. I dont need openfiledialog. How to solve it? It would the best if i can put .txt file in same folder with .exe and some cmd will know that it should open file only from this folder.. just guessing... thanks in further

What I have tried:

Dim FILE_NAME As String = "C:\FileName.txt"

If System.IO.File.Exists(FILE_NAME) = True Then
    Process.Start(FILE_NAME)
Else
    MsgBox("File Does Not Exist")
End If
Posted
Updated 24-Jun-17 5:20am
v4
Rate this:
Please Sign up or sign in to vote.

Solution 1

Use a configuration file: Managing Application Settings (.NET)[^]
   
Comments
JeezyWonder 24-Jun-17 8:17am
   
Is it an app.config usage?
OriginalGriff 24-Jun-17 8:21am
   
Yes - the link does say that.
JeezyWonder 24-Jun-17 8:36am
   
too bad that mdsn doesnt provide too much code example with "configuration text files", honestly cant get the point of app.config
OriginalGriff 24-Jun-17 8:44am
   
That's because the code is trivial:
Dim myString as String = Properties.Settings.Default.MyStringSetting
...
Properties.Settings.Default.MyStringSetting = myNewStringValue
Properties.Settings.Default.Save()

The data is stored in an XML file so it's simple to edit outside the app if you need to.
JeezyWonder 24-Jun-17 9:40am
   
Yes, could you be so kind to provide example how to assign 8000 chars(i have them in text file now, so i will just copy) to the app.config string? then get it from it, when i need it in the code, like this? :
Dim myString as String = Properties.Settings.Default.MyStringSetting
myString = '8000 chars here
Properties.Settings.Default.MyStringSetting = myNewStringValue
Properties.Settings.Default.Save()
(then it will be saved in xml as i understnad? and how to get chars back from xml and assign to new var?)
OriginalGriff 24-Jun-17 9:52am
   
myString = '8000 chars here
Properties.Settings.Default.MyStringSetting = myString
Properties.Settings.Default.Save()
OriginalGriff 24-Jun-17 9:53am
   
But the way I would do it is to store the path to the file in the config file, rather than the file content.
JeezyWonder 24-Jun-17 9:56am
   
"ut the way I would do it is to store the path to the file in the config file, rather than the file content." Seems i understand, but can you show a simple example, please
OriginalGriff 24-Jun-17 10:05am
   
Oh come on!
A path is just a string, you know that. So just replace "myString" with the path to your file...
JeezyWonder 24-Jun-17 9:55am
   
Ok, and how to get that 8000 saved chars back for assigning to a new var?
Rate this:
Please Sign up or sign in to vote.

Solution 2

To get the system drive in C# you can use:
var p = System.IO.Path.GetPathRoot(Environment.GetFolderPath(Environment.SpecialFolder.System));
In Vb.NET:
Dim p = System.IO.Path.GetPathRoot(Environment.GetFolderPath(Environment.SpecialFolder.System))
To get the path where the exe is:
Dim strPath As String = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().CodeBase)
Your will have to remove the begin of the string "file:\\" though.
   
v3
Comments
JeezyWonder 24-Jun-17 10:33am
   
So "p" will contain a System drive info? how to use it?
Dim FILE_NAME As String = "C:\FileName.txt"
RickZeeland 24-Jun-17 10:39am
   
It will be a string which contains the drive letter, e.g. "D:".
Just concatenate with your filename, or better: use Path.Combine()
JeezyWonder 24-Jun-17 10:47am
   
Dim FILE_NAME As String = p +"\FileName.txt" - Will work?
RickZeeland 24-Jun-17 10:49am
   
Just try don't be afraid :) might be that you don't have to use the '\' in the filename, so it is better to use Path.Combine() which will add that automatically if needed.
JeezyWonder 24-Jun-17 11:20am
   
Thanks very much, one more qeustion, drive problem solved with your provided help, but how to resolve folder path problem? User can exctract my folder with my .exe and .txt files on just "C:\" or On "C:\Detskop" or another C drive folder, thanks a lot
RickZeeland 24-Jun-17 12:32pm
   
If the .exe and .txt files are in the same location, you don't need a path at all, just use the bare filename !
JeezyWonder 24-Jun-17 12:49pm
   
gooood, its want im searching for, much thanks sir
JeezyWonder 24-Jun-17 13:00pm
   
to be sure it will look like this: Dim FILE_NAME As String = "\FileName.txt" ?
RickZeeland 24-Jun-17 13:15pm
   
Close, but you don't need the backslash.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100