Return FileName Only when using OpenFileDialog - vb.net

I am using the following method to browse for a file:
OpenFileDialog.ShowDialog()
PictureNameTextEdit.Text = OpenFileDialog.FileName
Is there a way get ONLY the file name?
The FileName method returns the entire path and file name.
i.e. I want Foo.txt instead of C:\SomeDirectory\Foo.txt

Use Path.GetFileName(fullPath) to get just the filename part, like this:
OpenFileDialog.ShowDialog()
PictureNameTextEdit.Text = System.IO.Path.GetFileName(OpenFileDialog.FileName)

OpenFileDialog.ShowDialog()
PictureNameTextEdit.Text = System.IO.Path.GetFileName(OpenFileDialog.FileName)

C++ code for obtain filename and complete path in OpenFileDialog:
textBox1->Text = OpenFileDialog1->FileName; //complete path
textBox1->Text = System::IO::Path::GetFileName(OpenFileDialog1->FileName); //filename

Suppose that I did select word2010 file named as "MyFileName.docx"
This is for ONLY the selected file extension "including the dot mark, f.e (.docx)"
MsgBox(System.IO.Path.GetExtension(Opendlg.FileName))
And this for the selected File name without extension: (MyFileName)
MsgBox(System.IO.Path.GetFileNameWithoutExtension(Opendlg.FileName))
and you can try the other options for the "PATH Class" like: GetFullPath,GetDirectoryName ...and so on.

if you want just the selected name without Extension you can try this code
Imports System.IO
PictureNameTextEdit.Text = Path.GetFileNameWithoutExtension(OpenFileDialog1.Fi‌​leName)
thanx

//Following code return file name only
string[] FileFullPath;
string FileName;
objOpenFileDialog.Title = "Select Center Logo";
objOpenFileDialog.ShowDialog();
FileFullPath = objOpenFileDialog.FileNames[0].ToString().Split('\\');
FileName = FileFullPath[FileFullPath.Length - 1]; //return only File Name
//Use following code if u want save other folder ,
// following code save file to CenterLogo folder which inside bin folder//
System.IO.File.Copy(OFD.FileName, Application.StartupPath +
"/CenterLogo/" + FileName, true);

Use SafeFileName instead of FileName and it will return a name (and extension) without path.

Use this code to put the filename in PictureNameTextEdit:
OpenFileDialog.ShowDialog()
PictureNameTextEdit.Text = OpenFileDialog.SafeFileName

Related

Where and how do I make another folder to put text files so they work when I export?

I have a folder with my text files that can read and write and work in eclipse. But, when I export to jar, it fails because the files are not found, meaning they are not exported and I don't know how to make eclipse do that. I'm sure the solution is out there, but I don't know exactly what I'm searching for. Do I make a relative directory and how? Or another source folder? What exactly do I need to do?
This shows I have a folder called conf where my files are stored but it is not there on export.
Scanner in = new Scanner(new FileReader("conf/Admins.txt"));
FileWriter out = new FileWriter("conf/CurrentUser.txt");
int id = 0;
String name = "";
String pass = "";
boolean found = false;
while(in.hasNext()) {
id = in.nextInt();
name = in.next();
pass = in.next();
if(id == userID) {
out.write(id + " " + name + " " + pass + "\n");
found = true;
break;
}
}
All I had to do was once exported, put my conf file in the same place as the exported jar file. I don't know if, theres a better way but this is a win for me.

Get only file name from openfiledialog [duplicate]

I am using the following method to browse for a file:
OpenFileDialog.ShowDialog()
PictureNameTextEdit.Text = OpenFileDialog.FileName
Is there a way get ONLY the file name?
The FileName method returns the entire path and file name.
i.e. I want Foo.txt instead of C:\SomeDirectory\Foo.txt
Use Path.GetFileName(fullPath) to get just the filename part, like this:
OpenFileDialog.ShowDialog()
PictureNameTextEdit.Text = System.IO.Path.GetFileName(OpenFileDialog.FileName)
OpenFileDialog.ShowDialog()
PictureNameTextEdit.Text = System.IO.Path.GetFileName(OpenFileDialog.FileName)
C++ code for obtain filename and complete path in OpenFileDialog:
textBox1->Text = OpenFileDialog1->FileName; //complete path
textBox1->Text = System::IO::Path::GetFileName(OpenFileDialog1->FileName); //filename
Suppose that I did select word2010 file named as "MyFileName.docx"
This is for ONLY the selected file extension "including the dot mark, f.e (.docx)"
MsgBox(System.IO.Path.GetExtension(Opendlg.FileName))
And this for the selected File name without extension: (MyFileName)
MsgBox(System.IO.Path.GetFileNameWithoutExtension(Opendlg.FileName))
and you can try the other options for the "PATH Class" like: GetFullPath,GetDirectoryName ...and so on.
if you want just the selected name without Extension you can try this code
Imports System.IO
PictureNameTextEdit.Text = Path.GetFileNameWithoutExtension(OpenFileDialog1.Fi‌​leName)
thanx
//Following code return file name only
string[] FileFullPath;
string FileName;
objOpenFileDialog.Title = "Select Center Logo";
objOpenFileDialog.ShowDialog();
FileFullPath = objOpenFileDialog.FileNames[0].ToString().Split('\\');
FileName = FileFullPath[FileFullPath.Length - 1]; //return only File Name
//Use following code if u want save other folder ,
// following code save file to CenterLogo folder which inside bin folder//
System.IO.File.Copy(OFD.FileName, Application.StartupPath +
"/CenterLogo/" + FileName, true);
Use SafeFileName instead of FileName and it will return a name (and extension) without path.
Use this code to put the filename in PictureNameTextEdit:
OpenFileDialog.ShowDialog()
PictureNameTextEdit.Text = OpenFileDialog.SafeFileName

Use of (“Content-Disposition”, “inline; filename=” + fileName +"") not showing the file name

file name: abc.aspx
my code:
string Path = #"E:\documents\Data20160129110355.xls";
HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
HttpContext.Current.Response.AppendHeader("Content-Disposition", "inline; filename=Data20160129110355.xls");
HttpContext.Current.Response.TransmitFile(Path);
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.Close();
On click of "Open" it will open the file successfully but the header of the file are not showing "Data20160129110355.xls". It's showing abc.aspx.
How to solve this problem please reply.
Hope this will help:
Content-Disposition:What are the differences between "inline" and "attachment"?
replace 'inline' with 'attachment'.
UP: try this:
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment; filename=Data20160129110355.xls");

How to read conf.properties file?

I want to read conf.properties file but but not by giving a hard coded path, so how can I fins it? I have use the below line but its written a null value but the path was correct.
InputStream inputStream = ReadPropertyFile.class.getResourceAsStream("configuration/conf.properties");
Assuming you have configuration folder in your project & that folder contains conf.properties
Use following in your function -
String projdir = System.getProperty("user.dir");
String propfilepath = projdir+"\\config\\"+"conf.properties";
Properties p = new Properties();
p.load(new FileInputStream(propfilepath ));
String value = p.getProperty("test");
System.out.println(value); // It is returning me a value corresponding to key "test"

UnauthorizedAccessException when programatically accessing mp3 file in WinRT XAML app

Problem accessing mp3 files in WinRT app.
'System.UnauthorizedAccessException' occurs when app tries to open a mp3 file by name in the same folder as a file returned by FileOpenPicker. Put another way, the user picks an info file in Documents with the same name as a mp3 file. App opens the info file just fine but cannot open the mp3 file.
For example: I have a pair of files (file1.info) and (file1.mp3). A filepicker allows selecting a (*.info) file.
The user selects (file1.info). The app then opens both (file1.info) and (file1.mp3). Both files reside in a DocumentsLibrary folder, but are NOT in the MusicLibrary. The problem is when I try to open (file1.mp3) I get the 'UnauthorizedAccessException'.
To prepro the issue:
Files:
Copy an mp3 file to Documents.
Create a text file with the same base name as the mp3 file and change its extension to .info.
In Package.appxmanifest > Declarations add a 'File Type Associations' declaration. Check 'Open is safe'. Add
supported file types '.mp3' and '.info'. Leave 'Content type' empty.
Code:
Dim file as StorageFile
Dim fileopenpicker As FileOpenPicker
Dim infofile As StorageFile
Dim mp3file As StorageFile
Dim filename As String
fileopenpicker = New FileOpenPicker()
fileopenpicker.FileTypeFilter.Add(".info")
fileopenpicker.FileTypeFilter.Add(".mp3")
fileopenpicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary
file = Await fileopenpicker.PickSingleFileAsync()
If file.Path.EndsWith(".info") Then
infofile = file
filename = file.Path.Substring(0, file.Path.Length - 4) & "mp3"
' This command fails with 'System.UnauthorizedAccessException'
mp3file = Await StorageFile.GetFileFromPathAsync(filename)
Else 'file is an mp3 file
mp3file = file
filename = file.Path.Substring(0, file.Path.Length - 3) & "info"
' This command succeeds!
infofile = Await StorageFile.GetFileFromPathAsync(filename)
End If
So it appears that there is some specific problem with opening an mp3 file when the file is not actually chosen by the fileopenpicker.
I checked this issue with an app that has the capability Documents Library and the filetypes .mp3 and .info declared. I figured out that it seems to be a very strange bug. If you pass the path to the documents library folder using an uppercase drive letter after having opened a FileOpenPicker you get an UnauthorizedAccessException. Using the path with a lowercase drive letter works. Strangely you can use an uppercase drive letter before having opened a FileOpenPicker.
So the workaround is to lowercase the path.
Here's the code I used (C#):
// Trying to get some files from the documents library
// Note: F:\Program Data is my primary documents library folder
string mp3FilePath = #"F:\Program Data\2Mann1Maus.mp3";
// This works even if the drive letter is uppercase
StorageFile file1 = await StorageFile.GetFileFromPathAsync(mp3FilePath);
// It also works with a lowercase drive letter
string infoFilePath = #"f:\Program Data\2Mann1Maus.info";
StorageFile file2 = await StorageFile.GetFileFromPathAsync(infoFilePath);
FileOpenPicker fileopenpicker = new FileOpenPicker();
fileopenpicker.FileTypeFilter.Add(".info");
fileopenpicker.FileTypeFilter.Add(".mp3");
fileopenpicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
StorageFile file = await fileopenpicker.PickSingleFileAsync();
if (file.Path.EndsWith(".info"))
{
string filename = file.Path.Substring(0, file.Path.Length - 4) + "mp3";
// This works
string testFileName1 = filename.Substring(0, 1).ToLower() +
filename.Substring(1, filename.Length - 1);
StorageFile mp3file1 = await StorageFile.GetFileFromPathAsync(testFileName1);
// This works as well
string testFileName2 = filename.ToLower();
StorageFile mp3file2 = await StorageFile.GetFileFromPathAsync(testFileName2);
// This does cause an UnauthorizedAccessException
StorageFile mp3file3 = await StorageFile.GetFileFromPathAsync(filename);
}
else
{
StorageFile mp3file = file;
String filename = file.Path.Substring(0, file.Path.Length - 3) + "info";
// This works
string testFileName1 = filename.Substring(0, 1).ToLower() +
filename.Substring(1, filename.Length - 1);
StorageFile infoFile1 = await StorageFile.GetFileFromPathAsync(testFileName1);
// This works as well
string testFileName2 = filename.ToLower();
StorageFile infoFile2 = await StorageFile.GetFileFromPathAsync(testFileName2);
// This does cause an UnauthorizedAccessException
StorageFile infoFile3 = await StorageFile.GetFileFromPathAsync(filename);
}