I am importing a source file and processing it and then after that I have to save it in a new location. I have created the syntax for importing file, tell me the syntax to save it to a new location. One is when I am calling the constructor to give the path of import file, then I can also give the path for the output location. But don't know how to implement it. Please tell.
You can use a SaveFileDialog pretty much like this:
using ( var dlg = new SaveFileDialog() )
{
if ( dlg.ShowDialog() == DialogResult.OK )
{
//SAVE THE OUTPUT
//DEPENDING ON THE FORMAT, YOU MAY WANT TO USE
//File.WriteAllBytes(dlg.FileName, yourBytes);
//File.WriteAllText(dlg.FileName, yourText);
//File.WriteAllLines(dlg.FileName, yourStringArr);
//OR ANY OTHER CODE YOU WANT TO USE TO PERSIST YOUR DATA
}
//else the user clicked Cancel
}
Also, you can set a default extension, a default path and more. Look up SaveFileDialog's information on MSDN
As your importing the file you could use the StreamWriter base class to write it to a file that the end user designates in a either a text box or a file upload box.
Related
I'm using OpenFileDialog (.Net Framework 4, Windows 10) and I've noticed that it will allow the user to specify a URL as the file name (e.g., http://somewebsite/picture.jpg). This is very useful for my application, so I don't intend to disable it. The way it works is downloading the file into the user's temp directory and returning the temporary file name in the dialog's Filename property. This is nice, except for the fact that the user starts to build up garbage in his/her temp directory.
I would like to tell when a file was downloaded by the OpenFileDialog class (as opposed to a previously existing file), so I can clean up by deleting the file after use. I could check if the file's directory is the temp directory, but that's not very good since the user might have downloaded the file him/herself.
I've tried intercepting the FileOK event and inspect the Filename property to see if it is an HTTP/FTP URI, but despite what the documentation says ("Occurs when the user selects a file name by either clicking the Open button of the OpenFileDialog") it is fired after the file is downloaded, so I don't get access to the URL: the Filename property already has the temporary file name.
EDIT: This is an example of what I'like to do:
Dim dlgOpenFile As New System.Windows.Forms.OpenFileDialog
If dlgOpenFile.ShowDialog(Me) <> Windows.Forms.DialogResult.OK Then Return
''//do some stuff with dlgOpenFile.Filename
If dlgOpenFile.WasAWebResource Then
Dim finfo = New IO.FileInfo(dlgOpenFile.Filename)
finfo.Delete()
End If
In this example, I've imagined a property to dlgOpenFile "WasAWebResource" that would tell me if the file was downloaded or originally local. If it's the first case, I'll delete it.
There's no obvious way to do this, but as a workaround, how about checking where the file lives? It looks like by default this dialog downloads files to the users Temporary Internet Files directory, so you could introduce some code that looks something like this:
FileDialog dialog = new OpenFileDialog();
if (dialog.ShowDialog() == DialogResult.OK)
{
string temporaryInternetFilesDir = Environment.GetFolderPath(System.Environment.SpecialFolder.InternetCache);
if (!string.IsNullOrEmpty(temporaryInternetFilesDir) &&
dialog.FileName.StartsWith(temporaryInternetFilesDir, StringComparison.InvariantCultureIgnoreCase))
{
// the file is in the Temporary Internet Files directory, very good chance it has been downloaded...
}
}
I have this web application project which requires a better user-interface and I am doing it by C#.
I already have my html file with JS done but I need some data from user.
In my JS embedded in HTML file, I used the code below to find the file on local driver and get the data from that excel file and then put all these data into an array.
var excel = new ActiveXObject("Excel.Application");
var excel_file = excel.Workbooks.Open(file1);
var excel_sheet = excel.Worksheets("Sheet1");
However, the "file1" you see above seems to require a full name path,say "C:\test.xls" in my case.
I am new to C# and just built a button on my form design, by clicking the button, I seem to be able to browse my local file.
private void button1_Click(object sender, EventArgs e)
{
int size = -1;
DialogResult result = openFileDialog1.ShowDialog(); // Show the dialog.
if (result == DialogResult.OK) // Test result.
{
string file = openFileDialog1.FileName;
try
{
string text = File.ReadAllText(file);
size = text.Length;
}
catch (IOException)
{
}
System.Diagnostics.Process.Start(file);
}
Console.WriteLine(size); // <-- Shows file size in debugging mode.
Console.WriteLine(result); // <-- For debugging use.
}
So, my question:
How can I get this kind of full file path of an uploaded file in C# ?
And furthermore, it would be awesome if some one can tell me how to get this value into my javascript or HTML!
Thank you in advance
You won't be able to depend on getting the full path. In the end, the browser only needs to multi-part encode the physical file and submit it as a form post (of sorts). So once it has it's location and has encoded it -- it's done using it.
It's considered a security risk to expose the file structure to Javascript/HTML (ie., the internet).
Just an update.
I used another logic and it worked as expected. Instead of getting absolute file path , I managed to open the file , save it as somewhere and make my html call find that file path no matter what.
I have a program which contains a richTextBox and a Button. Upon clicking the button, the program gets the text from richTextBox and saves it to a predefined location stored in a string variable.
If I change this location to C drive which is my System Drive, it won't allow me to do it and throws a System.UnauthorizedAccessException.
I am also the admininstrator of my PC. Is there any way that I can get permissions or work something out to solve this issue? Thanks in advance.
I use the following code
private void button1_Click(object sender, EventArgs e)
{
string temp = location;
System.IO.StreamWriter file = new System.IO.StreamWriter(temp);
file.WriteLine(this.richTextBox1.Rtf);
file.Close();
}
Trying to write files directly to the root of the c: drive often cause problems, such as the exception you're seeing.
Try storing your file somewhere else. A good way of getting a safe folder is to use the SpecialFolder enumeration: (change it to Desktop, MyDocuments, or whatever might be appropriate in your case)
string temp
= Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "myFile.txt")
System.IO.StreamWriter file = new System.IO.StreamWriter(temp);
Considering your update, try this instead:
string temp
= Path.Combine(#"C:\Users\MuhammadWaqas\SkyDrive", "myFile.txt");
As pcnThird suggested, you could save it to "myFile.rtf" instead and take advantage of the file type, assuming you're going to be opening the file and reading the contents outside of your app.
You have to specify a file name instead of a folder, try changing location to "C:\Users\MuhammadWaqas\SkyDrive\test.txt"
Check your location. You might be trying to write to a non existent or restricted location
According to this msdn article one of the sources for InitialDirectory property used in FileDialog is:
A path that was previously used in the program, perhaps retained from the last directory or file operation.
...
So if you selected your first file from folder x, the next time you try to select a file it will open up the FileDialog with with folder x selected (saving you having to navigate there).
Playing around with notepad this seems to carry across opening a file, saving a file, opening a file and even when printing with "Microsoft XPS Document Writer" which brings up it's own dialog.
So my question is where is this value stored between dialogs? I would like to be able to see what it is and potentially change it? The specific area i would like to change it is in the "Microsoft XPS Document Writer" printer which brings up it's own dialog. So it's not as simple as just setting the initalDirectory Value.
It's stored in the registry, somewhere in HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\ComDlg32\ (LastVisitedPidlMRU).
You should take a look at this link:
MRU locations are what you are looking at!
Here's a way for accessing it:
var openFileDialog1 = new OpenFileDialog();
string path = openFileDialog1.InitialDirectory;
// you can change path if you want
openFileDialog1.InitialDirectory = path;
// after you are donw you can display you dialog
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
// do something
}
Another way is to use Directory.SetCurrentDirectory method which sets the application's current working directory
And from Microsoft website, it is stored at this location in the registry:
//The MRU lists for Windows Explorer-style dialog boxes are stored by file type for each user in the following registry key:
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\ComDlg32\OpenSaveMRU
hope this helps
I'm creating a richtextbox editor and need to put a save function as well as a save as. I can easily do a save as function by using the savefiledialog but im not sure how to save without this. Can anyone help?
Create a field somewhere, say string filename. Set it to null initially.
When a document is opened, store the file name in a filename.
When a document is saved through Save As, also store this file name in filename.
When Save is invoked, check the value of filename. If it is null, invoke Save As instead. If it is not null, save to the file name specified in filename.
The way this usually works is to keep track of the file name the user either opened or saved as.
Then, when they use the Save function, simply save to the file name that was previously specified. If no file has been specified, then show the Save As.
Isn't "Save" simply the following (in pseudocode)?
Save() =
WriteTo(oldfilename)
SaveAs() =
stream = OpenDialog()
oldfilename = stream.filename
Save()