I have a C# winForms program that is designed to run off a USB drive from multiple PCs. When I connect the USB drive to another PC, the drive letter change. How can I make my drive letter consistent across all PCs, or make my program truly "portable". I need the drive letter because I have a SQLite Database datasource pointed to a certain directory. And i also have PDF files opening from a certain directory with a specific drive letter. Thanks
Asked
Active
Viewed 1,051 times
-5
-
3Why do you need the drive letter in the first place? – Patrick Hofman Nov 03 '14 at 15:29
-
you can try letters for usb drive with try-catch. your file path in the usb drive doesn't change. it will eventually catch the right condition. – İsmet Alkan Nov 03 '14 at 15:29
-
I need the drive letter because I have a SQLite Database datasource pointed to a certain directory. And i also have PDF files opening from a certain directory with a specific drive letter. – Onlytito Nov 03 '14 at 15:37
2 Answers
2
You can get the executing assembly path.
string path = (new System.Uri(Assembly.GetExecutingAssembly().CodeBase)).AbsolutePath;
From there, you can obtain the drive letter (path root):
string drive = Path.GetPathRoot(path);
If your file is in the same directory as the executable, you can get the file path like this:
string directory = Path.GetDirectoryName((new System.Uri(Assembly.GetExecutingAssembly().CodeBase)).AbsolutePath);
string databaseFile = Path.Combine(directory, "filename.dbf");
![](../../users/profiles/993547.webp)
Patrick Hofman
- 143,714
- 19
- 222
- 294
![](../../users/profiles/33082.webp)
Michael G
- 6,452
- 1
- 38
- 59
-
2Ahh.. @PatrickHofman ... again. Care to enlighten us as to what the issue is with this answer? – BenjaminPaul Nov 03 '14 at 15:30
-
OP wants to know how to fix the drive letter. The answer given is 'this is how to get the executable path'. They are not related. – Patrick Hofman Nov 03 '14 at 15:33
-
1@Benjamin the issue is that we can guess, but that it's entirely unclear what OP is asking. If it is _"How can I get the directory that my executable is in?"_, then that is a duplicate which has been answered tens of times on the site and should not be answered but flagged as such instead. – CodeCaster Nov 03 '14 at 15:34
-
1I am answering this question: "How can I make my drive letter consistent across all PCs, or make my program truly "portable".", or rather my interpretation of this question. – Michael G Nov 03 '14 at 15:35
-
2No... he says "OR.. make the program truly portable"... As this removes the need for hard coded drive paths... Its a valid answer. – BenjaminPaul Nov 03 '14 at 15:35
-
Basically this is the answer that the OP needs, not what he asked. – David Crowell Nov 03 '14 at 15:35
-
Then explain the relation between the assembly path and the need to get the drive letter. Currently it doesn't say anything. – Patrick Hofman Nov 03 '14 at 15:35
-
I updated the answer to include the relevant code to obtain the drive letter. – Michael G Nov 03 '14 at 15:37
-
@MichaelG: I updated your answer a little to get the directory and point to the correct file. – Patrick Hofman Nov 03 '14 at 15:40
-
1
-
0
I guess you need the directory of your application.
string path = System.IO.Directory.GetCurrentDirectory();
(if you call SetCurrentDirectory()
, use this: System.IO.Path.GetDirectoryName(new System.Uri(System.Reflection.Assembly.GetExecutingAssembly().CodeBase).LocalPath);
)
Do you want your drive? (Drive's always 1 letter)
path.Substring(0, 2);
If not one letter,
path.Substring(0, path.IndexOf('\\') + 1);
![](../../users/profiles/2622807.webp)
joppiesaus
- 4,888
- 3
- 21
- 35
-
2You have to be careful using this, because someone can call SetCurrentDirectory() – Michael G Nov 03 '14 at 15:31
-
1I need the drive letter because I have a SQLite Database datasource pointed to a certain directory. And i also have PDF files opening from a certain directory with a specific drive letter. – Onlytito Nov 03 '14 at 15:37
-