Where does it put my text file in on android?

Emon Haque 3,176 Reputation points
2021-02-02T15:19:11.417+00:00

Here's my MainPage.xaml:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:VM="clr-namespace:Emon"
             x:Class="Emon.MainPage">
    <ContentPage.BindingContext>
        <VM:MainVM/>
    </ContentPage.BindingContext>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <StackLayout Margin="20,20,20,0">
            <Entry Placeholder="First Name" Text="{Binding FirstName}"/>
            <Entry Placeholder="Last Name" Text="{Binding LastName}" />
            <Button Text="Send Name" Command="{Binding Send}"/>
        </StackLayout>
        <ListView Grid.Row="1" ItemsSource="{Binding Infos}"/>
    </Grid>
</ContentPage>

When I click Send Button, it sends FirstName and LastName to a Server and the Server, in response, sends back some string to the android client. Android client then stores the info, sent by server, in a text file, Storage.txt. Here's all the logic in ViewModel, MainVM.cs:

public class MainVM : INotifyPropertyChanged
{
    string fName, lName;
    public string FirstName
    {
        get { return fName; }
        set { fName = value; Send.ChangeCanExecute(); }
    }
    public string LastName
    {
        get { return lName; }
        set { lName = value; Send.ChangeCanExecute(); }
    }
    public ObservableCollection<string> Infos{ get; set; }
    public Command Send { get; set; }
    public event PropertyChangedEventHandler PropertyChanged;
    public MainVM()
    {
        Send = new Command(send, canSend);
        Infos= new ObservableCollection<string>();
    }
    void send()
    {
        var sock = new Socket(SocketType.Stream, ProtocolType.Tcp);
        sock.Connect(new IPEndPoint(IPAddress.Parse("***.***.***.106"), 5555));
        sock.Send(Encoding.ASCII.GetBytes(FirstName + " " + LastName));
        var data = new byte[128];
        sock.Receive(data);
        var info = Encoding.ASCII.GetString(data);
        Infos.Add(info);
        sock.Shutdown(SocketShutdown.Both);
        sock.Close();
        sock.Dispose();

        var file = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), "Storage.txt");
        var writer = new StreamWriter(file, true, Encoding.ASCII);
        writer.WriteLine(info);
        writer.Close();
        writer.Dispose();

        FirstName = string.Empty; LastName = null;
        OnPropertyChanged(nameof(FirstName));
        OnPropertyChanged(nameof(LastName));
    }
    bool canSend() => !string.IsNullOrWhiteSpace(FirstName) && !string.IsNullOrWhiteSpace(LastName);
    void OnPropertyChanged(string name) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}

After finishing the send/receive operation I'd connected the android device to my computer via USB cable and found two folders, cache and files, in my app location, ie, This PC\V75\Internal storage\Android\data\com.companyname.emon BUT neither of those contain any file named Storage.txt.

Where does it save files?

Developer technologies | .NET | Xamarin
0 comments No comments
{count} votes

Accepted answer
  1. Alessandro Caliaro 4,196 Reputation points
    2021-02-02T15:54:35.14+00:00
    var file = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), "Storage.txt");
    

    what value does the "file" variable contain? I Think you have the path of the file inside it

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.